Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Strong Params:Params.permit返回未经许可的参数,尽管存在白名单_Ruby On Rails_Ruby_Parameters_Ruby On Rails 5_Strong Parameters - Fatal编程技术网

Ruby on rails Strong Params:Params.permit返回未经许可的参数,尽管存在白名单

Ruby on rails Strong Params:Params.permit返回未经许可的参数,尽管存在白名单,ruby-on-rails,ruby,parameters,ruby-on-rails-5,strong-parameters,Ruby On Rails,Ruby,Parameters,Ruby On Rails 5,Strong Parameters,UsersProfileController具有如下强参数: def user_profile_params params.permit(:age, :relations) # yes, I am not requiring user_profile. Just permitting attributes I need. end 创建操作通过父级(有一个并且属于关联)生成UserProfile 在UserProfiles中调用参数将返回: &l

UsersProfileController具有如下强参数:

    def user_profile_params
      params.permit(:age, :relations)
      # yes, I am not requiring user_profile. Just permitting attributes I need. 
    end
创建操作通过父级(有一个并且属于关联)生成UserProfile

在UserProfiles中调用参数将返回:

    <ActionController::Parameters 
      {"age"=>"23", 
       "relations"=>"3", 
       "subdomain"=>"api", 
       "format"=>:json, 
       "controller"=>"api/v1/user_profiles", 
       "action"=>"create"} 
     permitted: false>
“23”,
“关系”=>“3”,
“子域”=>“api”,
“格式”=>:json,
“控制器”=>“api/v1/用户配置文件”,
“操作”=>“创建”}
允许:false>
调用用户_profile_params,返回以下内容:

    user_profile_params:
      Unpermitted parameters: subdomain, format
      <ActionController::Parameters 
       {"age"=>"23", 
       "relations"=>"3", } 
      permitted: true>
user\u profile\u参数:
不允许的参数:子域,格式
"23", 
“关系”=>“3”,}
允许:正确>
当收到post请求时,我希望能够使用user_profile_params中的白名单参数创建user_profile。相反,UserProfiles中的
创建
操作失败,出现错误:
未允许的参数:子域,格式

这不是我所期望的。我希望user_profile_参数只包含允许的值,而忽略所有其他值

我可以将
:format
:subdomain
添加到允许的属性列表中,但这让我感觉有点不对劲


有人能解释一下发生了什么/我错过了什么吗

此消息只是一个警告,而不是一个错误/异常。如果你的模型没有被持久化,那是另一个原因

从:

处理未经许可的密钥

默认情况下,将使用未明确允许的参数键 已登录到开发和测试环境。在其他环境中 这些参数将被过滤掉并忽略

此外,可以通过更改 中的参数属性上的config.action\u controller.action\u 您的环境文件。如果设置为:log,则将显示未经允许的属性 如果设置为:引发,将引发异常

您可以在控制台中模拟它(
rails c
):

伪参数散列={
“年龄”=>“23岁”,
“关系”=>“3”,
“子域”=>“api”,
“格式”=>:json,
“控制器”=>“api/v1/用户配置文件”,
“操作”=>“创建”
} 
permited_params=ActionController::Parameters.new(伪_params_hash).permit(:年龄,:关系)
#=>不允许的参数:子域,格式“23”,“关系”=>“3”}允许:true>
user=user.create(permited_params)#使用permited params进行批量分配
#检查是否有错误
如果user.errors.any,则放置user.errors.messages?

如您所见,此消息不是由
用户.create
抛出的,而是在调用
.permit
时抛出的。

在我看来,您实际上并没有通过UsersProfileController中的创建块。@ChrisMoody:我不确定是否理解您的意思。如果您的意思是该方法没有被调用,那么这就不是真的。能够在UsersProfileController中单步执行
创建
操作,并发现在此特定行上调用
用户配置文件\u参数
时发生错误
parent.build\u用户配置文件(用户配置文件\u参数)
。你能解释一下你的意思吗?你是对的。实际上是来张贴错误是由于
User
model上的验证。感谢您抽出时间回答:)
    user_profile_params:
      Unpermitted parameters: subdomain, format
      <ActionController::Parameters 
       {"age"=>"23", 
       "relations"=>"3", } 
      permitted: true>
fake_params_hash = {
    "age"=>"23", 
    "relations"=>"3", 
    "subdomain"=>"api", 
    "format"=>:json, 
    "controller"=>"api/v1/user_profiles", 
    "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?