I am currently working with Rails 4 and Devise 3.2.2. Devise already creates all the routes and controllers for allowing users to sign up, but as with most applications I want to customize the user model and add additional fields. Let’s take a look at how we would go about making these changes by adding a first and last name to the user.
First off, we create the migration – nothing special here.
Now we actually need to show these fields on the edit form - we can do this by adding a view to app/views/devise/registrations/new.html.slim (I’m using slim as my templating engine, you can use haml or erb or whatever you choose). I usually open the devise gem (bundle open devise) and then copy the devise view into my application and modify it.
When you now try and sign up you should see the new custom form, but your custom fields will not be persisted. In Rails 3 we would need to add these fields to attr_accessor - which has been deprecated in Rails 4. Instead we need to customize the strong parameters for the devise controller action.
Customizing the RegistrationsController
If you do some digging through the devise gem you will find the code you care about in the registrations controller. Specifically, when we create an account this method is used to sanitize the parameters:
Similarly, when we update a user’s account this method is used to sanitize the parameters:
One solution would be to write and register your own sanitizer. I’m not a huge fan of this approach since it seems rather complicated and because you need to override Devise’s sanitizer in the application controller. A much simpler approach (in my opinion) is to simply override these 2 methods and specify the strong parameters directly.
We can do this by creating our own registrations controller (which extends Devise) and then customizing these methods as we choose.
Here I’m customizing both methods since I want users to be able to specify their first and last names both when signing up and when updating their profiles.
Now we need to tell Devise to use our registrations controller - we can do this in our config/routes.rb file.
That’s all you need - now you can very easily customize your user model with Devise. Happy coding.