简体   繁体   中英

Adding name attribute to `User` in Devise

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!

in your user model locate;

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

and add:name on the end

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

For 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

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.

Yes. Add :name to attr_accessible in User Model

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  

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
 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM