/ Authentication / Configure Devise

1. Allowing additional user attributes

Let say we want to add the most common attributes to our user which is first_name & last_name. To do this, we need to permit these parameters to devise registration controller. The simple way to override it is by creation our own RegistrationController which extends DeviseController itself.

  • So, go to controller directory, and create a file name registration_controller.rb and paste this code below:
    class RegistrationsController < Devise::RegistrationsController
      def sign_up_params
        params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
      def account_update_params
        params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
  • And then go to routes.rb and edit devise route to use this controller:
    devise_for :users, :controllers => { registrations: 'registrations' }
      as :user do
      	 get '/users/sign_out' => 'devise/sessions#destroy'


2. Edit Devise forms

All Devise default views/templates are located at app/views/devise. To specifically edit the registration form you can open up app/views/devise/registrations/new.html.erb. And to edit the user setting page you could open up  app/views/devise/registrations/edit.html.erb.  You could add your additional attributes there like this for example:

<div class="field">
  <%= f.label :first_name %><br />
  <%= f.text_field :first_name %>

<div class="field">
  <%= f.label :last_name %><br />
  <%= f.text_field :last_name %>

3. Edit Application Controller


before_filter :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
   devise_parameter_sanitizer.for(:sign_up) << :first_name << :last_name << :email << :password << :password_confirmation 
   devise_parameter_sanitizer.for(:account_update) << :first_name << :last_name << :email << :password << :password_confirmation 


No video for this post.

No issues for this post.

by ryzalyusoff