且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用设计添加额外的注册字段

更新时间:2023-12-02 08:49:10

好的,所以我所做的只是覆盖设计注册控制器,根据设计文档更新 routes.rb 以反映这一点,复制并粘贴设计代码对于注册#create 原样,并将获取参数部分更改为使用我自己的强参数方法,就是这样.

OK, so what I did was just override the Devise registration controller, update routes.rb as per the devise docs to reflect this, copied and pasted the Devise code for registrations#create as is, and change the getting params part to use my own strong parameters method, and that was that.

class RegistrationsController < Devise::RegistrationsController

  def create
    build_resource(registration_params)

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        respond_with resource, :location => after_sign_up_path_for(resource)
      end
    else
      clean_up_passwords
      respond_with resource
    end
  end  

  private

  def registration_params
    params.require(:user).permit(:email, :title_id, :first_name, :last_name, 
      :province_id, :password, :password_confirmation)
  end

end