Adding name attribute to `User` in Devise

steffi2392 picture steffi2392 · Aug 17, 2011 · Viewed 14.8k times · Source

I'm trying to add a name attribute to the User model provided by Devise. I added a "name" column to my database, and changed the sign up view so that it asks for the user's name:

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <p><%= f.label :name %><br />
  <%= f.text_field :name %></p>

  <p><%= f.label :email %><br />
  <%= f.email_field :email %></p>

  <p><%= f.label :password %><br />
  <%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></p>

  <p><%= f.submit "Sign up" %></p>
<% end %>

<%= render :partial => "devise/shared/links" %>

It lets me log in, but when I check the database after doing so, name: nil. Do I have to add something to Devise's User controller or something? Thanks!

Answer

John Beynon picture John Beynon · Aug 17, 2011

in your user model locate;

 attr_accessible :email, :password, :password_confirmation, :remember_me

and add :name on the end