【发布时间】:2017-01-21 00:29:48
【问题描述】:
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 and belongs-to 关联)构建 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 中调用参数返回:
<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",
"relations"=>"3", }
permitted: true>
当一个帖子请求进来时,我希望能够使用 user_profile_params 中的白名单参数创建 user_profile。相反,UserProfiles 中的 create 操作失败并出现错误:Unpermitted parameters: subdomain, format。
这不是我所期望的。我希望 user_profile_params 只包含允许的值而忽略所有其他值。
我可以将:format 和:subdomain 添加到允许的属性列表中,但感觉有点不对劲。
有人可以解释发生了什么/我错过了什么吗?
【问题讨论】:
-
在我看来,您实际上并没有通过 UsersProfileController 中的创建块。
-
@ChrisMoody:不知道我明白你的意思。如果您的意思是该方法没有被调用,那么这是不正确的。能够进入 UsersProfileController 中的
create操作,并发现在此特定行parent.build_user_profile(user_profile_params)上调用user_profile_params时发生错误。你能解释一下你的意思吗?
标签: ruby-on-rails ruby parameters ruby-on-rails-5 strong-parameters