ruby-on-rails – 强参数:params.permit返回未经许可的参数,尽
UsersProfileController具有强大的参数,如下所示:
def user_profile_params params.permit(:age,:relations) # yes,I am not requiring user_profile. Just permitting attributes I need. end create动作通过父(has-one和belongs-to association)构建UserProfile def create parent = Parent.create_guest parent.build_user_profile(user_profile_params) if parent.save # do something else # handle error end end 在UserProfiles中调用params返回: <ActionController::Parameters {"age"=>"23","relations"=>"3","subdomain"=>"api","format"=>:json,"controller"=>"api/v1/user_profiles","action"=>"create"} permitted: false> 调用user_profile_params,返回: user_profile_params: Unpermitted parameters: subdomain,format <ActionController::Parameters {"age"=>"23",} permitted: true> 当发布请求时,我希望能够使用user_profile_params中的白名单参数创建user_profile.相反,UserProfiles中的创建操作失败并显示错误:未允许的参数:子域,格式. 这不是我的预期.我希望user_profile_params只包含允许的值并忽略所有其他值. 我可以添加:format和:subdomain到允许的属性列表,但有些东西感觉有点不对劲. 有人可以解释发生了什么/我错过了什么? 解决方法
此消息只是一个警告,而不是错误/异常.如果你的模型没有被持久化,那就是另一个原因.
从strong parameters docs:
您可以在控制台中模拟它(rails c): fake_params_hash = { "age"=>"23","action"=>"create" } permited_params = ActionController::Parameters.new(fake_params_hash).permit(:age,:relations) #=> Unpermitted parameters: subdomain,format <== warning logged to the console #=> <ActionController::Parameters {"age"=>"23","relations"=>"3"} permitted: true> user = User.create(permited_params) #mass assigment with permited params #check if there are errors puts user.errors.messages if user.errors.any? 如您所见,User.create不会抛出此消息,但是在调用.permit时. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- FlashBuilder的air sdk和FlashPlayer升级方法
- ruby-on-rails – Rails:创建子/父关系
- 3 rendering pipeline
- R:data.frame的聚合列
- ruby – Shikashi:ArgumentError:带有沙盒代码的参数数量
- mini-xml(mxml)开发手册--目录
- c# – 知道何时在UserControl后面的代码中更改“Datacontex
- Cocos2d-X 学习笔记 16 使用Base64算法对Cocos2dX自带CCUse
- 使用C#,如何知道文件夹是否位于网络上
- c# – 继承自非泛型LineSegmentClass的Generic Vector类