添加自定义参数以设计注册 - 不允许的参数 [英] Adding custom parameters to devise registration - unpermitted parameters
问题描述
我一直在尝试自定义设计注册方法以注册更多参数并更新更多(到目前为止运气不佳),但我总是收到Unpermitted parameters:
错误.我尝试使用此 使用设计添加额外的注册字段 和 https://github.com/plataformatec/devise#strong-parameters,但我无法克服.
I've been trying to customize the devise register method to register with more parameters and also update more(no luck so far), but I always get Unpermitted parameters:
error. I tried using this Adding extra registration fields with Devise and https://github.com/plataformatec/devise#strong-parameters, but I cant get over that.
我还考虑过创建一个新表来保存用户 ID 的外键,并在其中放入诸如 user_id、display_name、profile_picture
之类的东西,但是在尝试时我会遇到同样的问题从同一页面提交所有内容(与设计控制器混乱).
I've also thought about creating a new table to hold a foreign key the user id and put in there stuff like user_id, display_name, profile_picture
, but I would have the same problem when trying to submit everything from the same page(mess with the devise controller).
你对我如何解决这个问题有什么建议吗?我还需要发布什么?
Do you have any suggestions on how I can solve this? What else do I have to post?
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
推荐答案
看起来你只需要告诉 devise 应该允许哪些参数.默认情况下,devise 允许使用电子邮件(或用户名,取决于配置)、密码和 password_confirmation 参数.您只需要添加更多.
Looks like you just need to tell devise which parameters should be permitted. By default, devise permits the email (or username depending on configuration), password and password_confirmation params. You just need to add more.
设计文档建议采用懒惰的方式"进行设置.>
The devise documentation suggests a "lazy way" of setting this up.
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
然后文档说
如果您有嵌套属性(假设您正在使用 accepts_nested_attributes_for
),那么您需要将这些嵌套和类型告诉 devise.
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
方法.
Only if you need to override the registrations#create
action you should provide your custom route for devise. In that case, make sure you override the sign_up_params
method too.
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
本质上,您必须查看您的注册表单是如何发布参数的,以确定如何在控制器中配置强参数.请务必阅读强参数语法.
In essence, you'd have to look into how your sign up form is posting the parameters to figure out how to configure strong parameters in the controller. Make sure you read on strong parameters syntax as well.
希望能帮到你!
这篇关于添加自定义参数以设计注册 - 不允许的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!