Rails – Add Custom Fields to Devise User Model

Previous related posts:

By default, the users table in Devise only contains the email and password fields in user profile. Most likely we need to add our custom fields such that we can store more user information. The following steps show you how to add a username field in the users model.

1. Create the username field in the users table

  • rails g migration add_username_to_users username:string

 

2. Apply the new change on database

  • rake db:migrate

 

3. Add the username field to attr_accessible in app/model/user.rb

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username

 

4. Add the username field to the form inside the following views

  • app/views/devise/sessions/new.html.erb
  • app/views/devise/registrations/new.html.erb
  • app/views/devise/registrations/edit.html.erb
...
  <p><%= f.label :username %><br />
  <%= f.text_field :username %></p>
...

 

You should find the textbox for the username in the above forms now.

Done =)

Reference: Extend user model with own properties

Update @ 2012-02-13: If you couldn’t find the Devise views files, you need to generate them by the following command.

  • rails g devise:views

For more information, please refer to Rails – Customize Devise’s Views. Thanks Eleni. =)

About these ads

17 thoughts on “Rails – Add Custom Fields to Devise User Model

  1. alex

    Simple explanation. Thank you for this. I have a question/comment, however.

    While adding a username seems straight-forward and indeed necessary, adding additional fields may warrant another table in the database. Say you want to store more items and do this method… you may end up with a table that looks like:
    (:id, :email, :password, :password_confirmation, :remember_me, :username, :permissions, :image, :hometown, :website, :aboutme, :interests, :sexual_orientation) etc, bla bla bla. So whenever a simple loged_in? gets called, it’ll pull information from a massive table, ready to go. If instead you create a separate table, such as user_info, it will only recall the id, and doesn’t need to pull in unnecessary information. This would be a one-to-one relationship and can share the same ID as the user. Of course, you wouldn’t want your username in this table. It would be cool to add onto this simple post with instructions to create a new table and call from the edit/new/show view.

    Let me know if this isn’t accurate!

    Reply
    1. ykyuen Post author

      Hi Alex,

      Thanks for your comment. you made a good point on the schema design and it would be good to normalize the database, which is in this case, separating the user info table with the user login table just like what u said. If we have a very large system and performance is our big concern, we have to pay more attention to the schema design.

      but usually i dun want to make the post too long and in this post i just want to focus on adding a field in the Devise user table. If i have time, i would make another post to demonstrate the table relationship in the Rails framework.

      Thanks again for your comment. =)

      Kit

      Reply
    2. spilld

      Alex,

      I really like this approach and can’t seem to wrap my head around the way to do this. Any chance you have some time to write out how this might be done in rails? That would be AWESOME.

      Reply
  2. Rajkumar.S

    Hai,
    i am a new bie to use rails, nice posting its very useful for me. please send me any sample rails 3 applications or tutorials for cancan, geokit, for google map integrations
    thankyou…..

    Reply
  3. Eleni

    Nice post fellow!

    I’m new and I couldn’t find those views mentioned. Whoever has the same issue and needs to generate devise views, here’s the explanation:
    As devise is an engine for Rails, it provides some views. If you want to overwrite them you can add those views to your project by running this command -> rails generate devise:views
    Regards

    Reply
  4. brianking319

    pls i have a question too am showing the email of user by using user.email but trying to use user.username or user.department of which i have added both department and username to devise database it doesn’t display them and never shows any errors how can i go about this

    Reply
    1. ykyuen Post author

      Devise itself does not have any view to show the user data. In your case, you have to create a UsersController, query the users table and render them in a view.

      Reference: StackOverflow – Ruby on rails, devise, how to set up a user profile

      You could also consider create a new controller which inherits the Devise controller and add your own customization.

      Reference: StackOverflow – Making changes to the devise user controller in rails?

      Hope the above information could help. =)

      Reply
  5. Pingback: Proper way to add a field to a devise User model which will fed in from 3rd party source | Stackforum

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s