Add custom fields in user model created using devise

  1. After generating the custom views using devise gem using:
rails generate devise:devise users

2. Adding custom fields inside users model

rails generate migration addNameToUser name:string

rake db:migrate

3. Now add the custom field inside the view i.e: views/users/registrations/edit.html.erb

div class="field">
  <%= f.label :name %> <br/>
  <%= f.text_field :name, autofocus: true %>
</div>

4. To render customer views created in views/users/ make change inside the devise configuration file: ie. /config/initializers/devise.rb are change the following :

config.scoped_views = true

5.  Now you have to make changes inside controller/application.rb to allow the fields :

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:name]) 
  end
end

 

Advertisements

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

Blog at WordPress.com.

Up ↑

%d bloggers like this: