Monday, August 11, 2014

Customizing Devise

As I use Devise gem for user authentication, sometimes I run into situations when it is necessary to customize devise's controllers. The simplest case is when I want to add an extra field to be passed through the form during the registration. For example, I want to add a user's name.

So what to do:

1. Create your own RegistrationsController which inherits from Devise::RegistrationsController and modify the update_sanitized_params with the value you want to pass (for example, name).

class RegistrationsController < Devise::RegistrationsController
    before_filter :update_sanitized_params, if: :devise_controller?

    def update_sanitized_params
        devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:name, :organization_name, :email, :password, :password_confirmation,
                :name)}
    end
end

2. Update the routes.rb like so:

devise_for :users, :controllers => { registrations: 'registrations' }

3. Update the app/views/devise/registrations/new.html.erb form accordingly, so you can pass the name parameter.

4. Do a little dance if the above works.

No comments :

Post a Comment