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

Ruby Build a Simple Ruby on Rails Application Creating an Authentication System Generating the Devise Views

Alessandro Treglia
Alessandro Treglia
4,537 Points

Editing 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

Hey 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
Alessandro Treglia
4,537 Points

I'm using Rails 4. I installed the gem 'protected_attributes' which let me put the attr_accessible. Still nothing

Ok, 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! :)

I'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 :)

Hey 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. :)

class 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.

If 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! :)

Well 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.

Oh 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!