Menu

Add Custom Field/Column to Devise with Rails

April 10, 2015 - Rails

In This We add Custom Column In “SignUp Page” :-

Steps To Add Custom Field/Column to Devise i.e :-

1) Under App Console Write Code :-

rails g migration add_name_to_user name:string

* ‘name’ is a column-name we give Col-name according to requirement.

2) After this write code :-

rake db:migrate

* This code create column in database.

3) We add Code to Controller for run :-

Go to -> Your-app -> app -> controllers -> application_controller.rb (Paste code Here)

before_filter :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :email, :password) }
end

4) Now We add Column in SignUp Page By :-

Go to -> YourApp -> app -> views -> devise -> registrations -> Add Code Both In ‘New.html.erb’ and ‘Edit.html.erb’ (Paste Code Here)

<div><%= f.label :name %><br />
<%= f.text_field :name, :class=> “form-control” %></div>

5) Add Server Side Validation by :-

Go to -> YourApp -> app -> models -> user.rb (Paste Code Here)

validates_presence_of :name

Thats It Enjoy…!!!!

Leave a Reply