Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Derek Saunders
5,167 PointsCreating a profile and adding custom content
I have finished the last part of creating a profile which is scoping. Now for the extra credit it says to add favorites or hobbies, but I want to add an about me to the profile so the user can add a little description of themselves, but don't know how. I would really appreciate any help to learn how to create a custom field like this. Thanks!
5 Answers
Dino Paškvan
Courses Plus Student 44,108 PointsWell, it's pretty much like adding a first or last name. You add a bio (or whatever you want to call it) to the devise migration:
t.text :bio
Then you run the migration. You add the bio to attr_accessible and then add it to the forms inside the devise views via simple_form (which will automatically use <textarea> elements for the input, since you've defined the bio as text in the migration.
And finally, you can display it on the profile page the same way you display the full name or anything else. These are the basic guidelines to point you in the right direction. So, just backtrack to how the first_name, last_name and profile_name were added and do the same thing for the bio/description. Check out Build a Simple Ruby on Rails Application > Creating an Authentication System > Generating the User Model and related videos.
Derek Saunders
5,167 PointsDino Paškvan thank you so much, I was making it much harder then it really is. Much appreciated!
Derek Saunders
5,167 PointsDino Paškvan I did everything you mentioned above and I keep getting an error that says no method error in devise/registrations#edit
Dino Paškvan
Courses Plus Student 44,108 PointsWhere exactly are you getting the error? Which file and it would be helpful if you could paste the relevant code.
Derek Saunders
5,167 PointsWell is there any way to create a page where the user can go to such as update my profile, and there will be fields there for your location, bio, etc.? here is my code under views/devise/registrations/edit
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %> <%= devise_error_messages! %>
<div><%= f.input :email %></div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div> <% end %>
<div><%= f.input :password %> <i>(leave blank if you don't want to change it)</i></div>
<div><%= f.input :password_confirmation %></div>
<div><%= f.input :current_password %> <i>(we need your current password to confirm your changes)</i></div>
<div><%= f.input :bio %></div>
<div><%= f.button :submit %></div> <% end %>
<h3>Delete Profile</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %></p>
<%= link_to "Back", :back %>
Dino Paškvan
Courses Plus Student 44,108 PointsAh, the problem is the migration. You'll have to make a new migration to actually add the field. So run this from the console rails generate migration AddBioToUsers.
That will generate a new migration in db/migrate. Open the newly generated file and edit it to look like this:
class AddBioToUsers < ActiveRecord::Migration
def change
add_column :users, :bio, :text
end
end
After that you can run rake db:migrate to add the field to the users table. After that, it should work, as long as you've added :bio to attr_accessible. And you should be able to edit all the user data on the users/edit route when you're logged in.
Derek Saunders
5,167 PointsAwesome thanks a lot Dino, will I also be able to add additional fields if I wanted to, to this migration such as location possibly?
Dino Paškvan
Courses Plus Student 44,108 PointsWell, if you already ran rake db:migrate you'll have to create another migration. But you can generate as many migrations as you want. Some planning is recommended, though. So, if you want to add 3 more fields, just do them in one migration. Something like rails generate migration AddABunchOfStuffToUsers and then just dump the fields you want to add into the new migration:
class AddABunchOfStuffToUsers < ActiveRecord::Migration
def change
add_column :users, :location, :string
add_column :users, :favmovies, :text
add_column :users, :yearsinprison, :integer
end
end
Of course, nothing is stoping you from generating a migration for every additional field, but some planning will leave things clean which is always helpful when you're collaborating with someone else.