0

I just followed this guide to add the columns 'firstname' and 'secondname' to the Devise User Model with the following commands.

rails generate migration add_firstname_to_user firstname:string
rails generate migration add_secondname_to_user secondname:string

and I aplied the changes with:

rake db:migrate

It worked ok, because I can see those fields in the console with User.all, but the problem I have now is that I don't see the attr_accessible field in app/model/user.rb.

So I just added the lines:

<div><%= f.label :first_name %><br />
  <%= f.text_field :firstname, autofocus: true %></div>

<div><%= f.label :second_name %><br />
  <%= f.text_field :secondname, autofocus: true %></div>

in new.html.erb in app/views/devise/registrations, but it isn't working, because I noticed that the firstname and secondname attributes are nil on the users I registered.

What can I do?, I guess is something about the attr_accessible step, but I couldn't find it.

Any help will be appreciated.

OiciTrap
  • 169
  • 7

2 Answers2

0

I may be confused by your post, but I believe (as mentioned in step 3 of your linked document) you need to add the following to your user.rb:

attr_accessible :firstname, :secondname
Arthur Frankel
  • 4,695
  • 6
  • 35
  • 56
  • Yeah, maybe I don't express very good, english is not my first language, but responding your comment... if I add attr_accessible :firstname, :secondname in my user.rb I got an error that says: "undefined method `attr_accessible' for #", when I try to enter to the "Sign Up" page... – OiciTrap Jul 27 '14 at 23:48
  • Yes, no mass assignment allowed in Rails 4.1. Use the permit method in your controller - see here: http://stackoverflow.com/questions/23437830/undefined-method-attr-accessible – Arthur Frankel Jul 28 '14 at 01:25
0

You need to configure permitted parameters for devise. In app/controllers/application_controller.rb add the following lines:

def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:firstname, :secondname, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :email, :password, :remember_me) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:firstname, :secondname ,:email, :password, :password_confirmation, :current_password) }
end

See that you can add multiple columns (fields) in one migration:

rails g migration add_fields_to_user firstname:string secondname:string

This will generate a file inside:

db/migrate/20152347823846(just numbers here)_add_fields_to_user.rb

and you also can edit that file to add fields and then run rake db:migrate.

I know it's late to respond, but better late than never.

JuanM.
  • 434
  • 1
  • 8
  • 19