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 trialAlessandro Treglia
4,537 PointsEditing user page
What am I doing wrong or missing?
I added
t.string :first_name
t.string :last_name
t.string :profile_name
to the devise_create_user.rb and then added in the devise registration new.html.erb
<div><%= f.label :first_name %><br /> <%= f.text_field :first_name %></div>
<div><%= f.label :last_name %><br /> <%= f.text_field :last_name %></div>
<div><%= f.label :profile_name %><br /> <%= f.text_field :profile_name %></div>
but for some reason keep getting an error on the server saying
NoMethodError in Devise::Registrations#new Showing C:/Users/Alessandro/my2cents/app/views/devise/registrations/new.html.erb where line #7 raised:
undefined method `first_name' for #<User:0x448fae8> Extracted source (around line #7): 4 5 6 7 8 9 10
<%= devise_error_messages! %>
<div><%= f.label :first_name %><br /> <%= f.text_field :first_name %></div>
<div><%= f.label :last_name %><br /> <%= f.text_field :last_name %></div>
9 Answers
Michael Williams
8,074 PointsHey Alessandro,
It might be because you didn't set the attr_accessible in your user model like:
attr_accessible :first_name, :last_name, :profile_name
Note this will only work if you are using rails 3. If that doesn't work let me know! :)
Alessandro Treglia
4,537 PointsI'm using Rails 4. I installed the gem 'protected_attributes' which let me put the attr_accessible. Still nothing
Michael Williams
8,074 PointsOk, Smart move! That's def one way to fix that but it doesn't always work.
So since you are using rails 4 with devise it's a little more complicated to fix. Have you pushed your app to github? I can look at it and help you from there! :)
Björn Steneram
5,523 PointsI'm looking for a answer on this question to. I run Rails 4 to and been trying different solutions but doesn't seem to get the data saved like it should be. So if someone got some good advice to solve this it would be lovely :)
Michael Williams
8,074 PointsHey Bjorn,
Can you post the code in your users controller? Also what data are you trying to save that isn't being saved?
The problem is most likely that you haven't added the data to your user params at the bottom of your controller. In Rails 4 they moved the attr_accessible params from the model to the controller. :)
Björn Steneram
5,523 Pointsclass ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email, :name, :city, :zip) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :name, :city, :zip) }
end
end
This is how my application controller looks.. i don't have a user controller at the moment.
Michael Williams
8,074 PointsIf you haven't made the users controller at all that's def the problem. When you make it you have to add a private method like
private
def user_params
params.require(:user).permit(:name, :username, :email, :name, :city, :zip)
end
At the bottom of your users controller. If you have your app on git send me a link to that so I can see you whole code. Let me know if that doesn't work! :)
Björn Steneram
5,523 PointsWell when your mention it.. it is a bit strange that i don't have a user controller, but i used devise to create the user model and it seems when i run that generator it doesn't create user controller for some reason, just user model and the views needed for devise to work.
I'll try to create a user controller then and see if i can get it to work.
Michael Williams
8,074 PointsOh yeah... Sorry I forgot you were using devise. I'm not sure if it does or not cause I don't use devise. But yeah let me know man!