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. =)
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!
LikeLike
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
LikeLike
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.
LikeLike
Hi spilld, i think just setting up a has_one relationship could help. See if the folllowing post is useful for you.
Devise has_one Relationships
LikeLike
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…..
LikeLike
Ruby on Rails Tutorial: Learn Rails by by Example
This is a very good tutorial written by Michael Hartl. I also start from it.
LikeLike
Thanks for saving me time!
LikeLike
you are welcome =)
LikeLike
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
LikeLike
I have updated the post. thanks for your note. =)
LikeLike
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
LikeLike
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. =)
LikeLike
How do i make the field required on the frontend so they cant leave username blank, and also that the username must be unique?
LikeLike
Required field:
For uniqueness:
add the following line in your model.rb
Reference: RailsGuides – Active Record Validations and Callbacks
LikeLike
if I have 2 devise User and Admin, as I can generate the views for both devises separately?
Thank you.
LikeLike
I am not very sure if it can be done. but i think the following post may help.
StackOverflow – Using Devise for Two Different Models but the same Login Form
LikeLike