Ruby on rails 注册时设计模型的附加参数
我正在rails应用程序中为我的用户使用Desive。当人们注册时,只有他们的电子邮件和密码。我如何访问控制器以允许更多参数,如名字和姓氏?Ruby on rails 注册时设计模型的附加参数,ruby-on-rails,ruby-on-rails-4,devise,Ruby On Rails,Ruby On Rails 4,Devise,我正在rails应用程序中为我的用户使用Desive。当人们注册时,只有他们的电子邮件和密码。我如何访问控制器以允许更多参数,如名字和姓氏? 谢谢您应该将参数添加到design\u parameter\u消毒剂的注册 这可以在您的应用程序\u controller.rb中完成 class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :d
谢谢您应该将参数添加到
design\u parameter\u消毒剂
的注册
这可以在您的应用程序\u controller.rb中完成
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :first_name
end
end
class ApplicationController < ActionController::Base
protected
def devise_parameter_sanitizer
User::ParameterSanitizer.new(User, :user, params)
end
end
另一种方法是创建继承自designe::ParameterSanitizer
class User::ParameterSanitizer < Devise::ParameterSanitizer
def sign_up
default_params.permit(:password, :password_confirmation, :email, :first_name, :last_name)
end
end
class ApplicationController < ActionController::Base
protected
def devise_parameter_sanitizer
User::ParameterSanitizer.new(User, :user, params)
end
end