Adding extra registration fields with Devise

前端 未结 8 606
情话喂你
情话喂你 2020-12-01 06:57

I am trying to add some extra fields to registrations#new. Since I only want extra data and do not need different functionality, I don\'t see why I need to override controll

8条回答
  •  -上瘾入骨i
    2020-12-01 07:21

    First expose the views

    rails generate devise:views users
    

    then edit config/initializers/devise.rb and change

    # config.scoped_views = false
    

    to

    config.scoped_views = true
    

    this will allow you to modify the views at app/views/users/registration.

    you will add the fields needed here, in both

    app/views/users/registration/edit.html.erb

    app/views/users/registration/new.html.erb
    

    Now we have to deal with rails mass assignment issue, go to application_controller.rb and add a before_filter

    before_filter :configure_permitted_parameters, if: :devise_controller?
    

    then add your fields + original fields to devise sanitization

    protected
    
        def configure_permitted_parameters
            # Fields for sign up
            devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password) }
            # Fields for editing an existing account
            devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :current_password, :gender) }
        end
    

    restart your web server and cross your fingers.

提交回复
热议问题