25

I'm trying to add a name attribute to the User model provided by Devise. I added a "name" column to my database, and changed the sign up view so that it asks for the user's name:

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <p><%= f.label :name %><br />
  <%= f.text_field :name %></p>

  <p><%= f.label :email %><br />
  <%= f.email_field :email %></p>

  <p><%= f.label :password %><br />
  <%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></p>

  <p><%= f.submit "Sign up" %></p>
<% end %>

<%= render :partial => "devise/shared/links" %>

It lets me log in, but when I check the database after doing so, name: nil. Do I have to add something to Devise's User controller or something? Thanks!

steffi2392
  • 1,345
  • 3
  • 19
  • 19

8 Answers8

19

Rails 3

in your user model locate;

 attr_accessible :email, :password, :password_confirmation, :remember_me

and add :name on the end

notapatch
  • 6,569
  • 6
  • 41
  • 45
John Beynon
  • 37,398
  • 8
  • 88
  • 97
  • 12
    what about rails4? since there're no such attr_accessible thing – NamiW Jun 18 '13 at 12:59
  • @namiheike Not sure what the answer for rails 4 is at the moment, but a quick fix would be to add gem 'protected_attributes' to your gemfile. Then you can use attr_accessible – jordan Nov 08 '13 at 06:29
  • For Rails 4.0 (and newer), with strong parameters, take a look at the **[Rails and Devise](https://github.com/RailsApps/rails-devise)** example application from the RailsApps project. – Daniel Kehoe Apr 18 '14 at 05:27
13

Rails 5 (in fact devise 4)

Tested for: rails 5.1.0 (devise 4.2.1)

There is no need to work with devise controllers.

Just add the following to your application_controller.rb:

before_action :configure_permitted_parameters, if:  :devise_controller?


protected

  def configure_permitted_parameters

    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])

  end

devise_parameter_sanitizer.for no longer works with Rails 5 (to be more correct, it is not supported in devise 4, which is the expected devise version in a Rails 5 context): use devise_parameter_sanitizer.permit to avoid undefined method 'for' for #<Devise::ParameterSanitizer error

notapatch
  • 6,569
  • 6
  • 41
  • 45
Varus Septimus
  • 1,510
  • 20
  • 13
10

Add this code to application_controller.rb

before_action :configure_permitted_parameters, if: :devise_controller?



protected
def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
  devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
end
Pervez
  • 525
  • 6
  • 16
6

For Rails 4

Use like this

protected
def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
  devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
end

Add additional fields at the end.

PrivateUser
  • 4,474
  • 12
  • 61
  • 94
2

Yes. Add :name to attr_accessible in User Model

fl00r
  • 82,987
  • 33
  • 217
  • 237
1

Write this code inside the ApplicationController class...

before_action :configure_permitted_parameters, if:  :devise_controller?

      protected

      def configure_permitted_parameters
        devise_parameter_sanitizer.for(:sign_up) << :name  
0

uncomment some of the generated code:

class Users::RegistrationsController < Devise::RegistrationsController
  before_filter :configure_sign_up_params, only: [:create]

  protected

  def configure_sign_up_params
    devise_parameter_sanitizer.for(:sign_up) << :name
  end
end
sberkley
  • 1,188
  • 1
  • 8
  • 4
0
 private

   def sign_up_params
     params.require(:user).permit(:name,:email,:gender,:age,:password,:password_confirmation) if params[:user].present?
   end

Add this in the controller that extends devise's default registration controller

Apoorv
  • 1,338
  • 1
  • 17
  • 18