我一直在尝试自定义设计寄存器方法以注册更多参数并更新更多(到目前为止没有运气),但我总是得到Unpermitted parameters:
错误。我尝试使用此Adding extra registration fields with Devise和https://github.com/plataformatec/devise#strong-parameters,但我无法克服这个问题。
我还考虑过创建一个新表来保存用户ID的外键并放入user_id, display_name, profile_picture
之类的内容,但是当我尝试从同一页面提交所有内容时我会遇到同样的问题(混乱的设计控制器)。
您对我如何解决这个问题有什么建议吗?我还有什么要发布的?
routes.rb
devise_for :users, controllers: { registrations: 'users/registrations' }
用户/ REGC
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_paramss
params.require(:user).permit(:email, :display_name, :terms_of_services, :profile, :password, :password_confirmation)
end
答案 0 :(得分:18)
看起来你只需要告诉设计应该允许哪些参数。默认情况下,设计允许电子邮件(或用户名取决于配置),密码和password_confirmation参数。你只需要添加更多。
devise documentation暗示了一种懒惰的方式"设置它。
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:display_name])
end
end
文档然后说
If you have nested attributes (say you're using accepts_nested_attributes_for), then you will need to tell devise about those nestings and types.
只有当您需要覆盖registrations#create
操作时,才应提供设计的自定义路由。在这种情况下,请确保您也覆盖sign_up_params
方法。
class Users::RegistrationsController < Devise::RegistrationsController
def create
# Your custom code here. Make sure you copy devise's functionality
end
private
# Notice the name of the method
def sign_up_params
params.require(:user).permit(:display_name, :email, :password, :password_confirmation)
end
end
从本质上讲,您必须了解您的注册表单如何发布参数,以确定如何在控制器中配置强参数。请务必阅读strong parameters语法。
希望它有所帮助!
答案 1 :(得分:9)
对于Devise 4.2.0,您可以通过将这些值添加到密钥来为用户表列出其他参数。默认情况下,设计会为您提供评论。我在下面添加了:avatar
# If you have extra params to permit, append them to the sanitizer.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute, :avatar])
end
答案 2 :(得分:0)
在我看来,这行得通:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:name, :last_name, :image,:email, :password, :password_confirmation, :current_password) }
end
end
答案 3 :(得分:0)
可接受的答案是说,配置应该放在applicationController中,但是它可以简单地放在用户注册控制器中,您可以指定只想为create方法运行它,而无需执行其他操作:
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
protected
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:enter_param_name_here])
end
end