简体   繁体   English

Rails 4 不允许的数组参数

[英]Rails 4 Unpermitted Parameters for Array

I have an array field in my model and I'm attempting to update it.我的模型中有一个数组字段,我正在尝试更新它。

My strong parameter method is below我的强参数方法如下

def post_params
  params["post"]["categories"] = params["post"]["categories"].split(",")

  params.require(:post).permit(:name, :email, :categories)
end

My action in my controller is as follows我在控制器中的操作如下

def update
  post = Post.find(params[:id]

  if post and post.update_attributes(post_params)
    redirect_to root_url
  else
    redirect_to posts_url
  end
end

However, whenever I submit the update the post, in my development log I see但是,每当我提交更新帖子时,在我的开发日志中我都会看到

Unpermitted parameters: categories

The parameters passed through is传递的参数是

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"auth token", "id"=>"10", 

"post"=>{"name"=>"Toni Mitchell", "email"=>"eileen_hansen@hayetokes.info", "categories"=>",2"}}

I want to think it has something to do with the fact that the attribute categories is an array since everything else looks fine.我想认为它与属性categories是一个数组这一事实有关,因为其他一切看起来都很好。 Then again, I could be wrong.再说一次,我可能是错的。 So, what's wrong with my code and why is not letting me save the categories field when clearly it is permitted to do so?那么,我的代码有什么问题,为什么在明确允许的情况下不让我保存类别字段? Thanks.谢谢。

Try this 试试这个

params.require(:post).permit(:name, :email, :categories => [])

(Disregard my comment, I don't think that matters) (无视我的评论,我认为不重要)

在轨道4中,那将是,

params.require(:post).permit(:name, :email, {:categories => []})

The permitted scalar types are String , Symbol , NilClass , Numeric , TrueClass , FalseClass , Date , Time , DateTime , StringIO , IO , ActionDispatch::Http::UploadedFile and Rack::Test::UploadedFile . 允许的标量类型是StringSymbolNilClassNumericTrueClassFalseClassDateTimeDateTimeStringIOIOActionDispatch::Http::UploadedFileRack::Test::UploadedFile

To declare that the value in params must be an array of permitted scalar values map the key to an empty array: 要声明params中的值必须是允许的标量值数组,请将键映射到空数组:

params.permit(:id => [])

This is what the strong parameters documentation on Github says: 这就是Github上强参数文档所说的:

params.require(:post).permit(:name, :email, :categories => [])

I hope this works out for you. 我希望这对你有用。

I had the same problem but in my case I had also to change from: 我有同样的问题,但在我的情况下,我还要改变:

<input type="checkbox" name="photographer[attending]" value="Baku">

to: 至:

<input type="checkbox" name="photographer[attending][]" value="Baku">

Hope this is helping someone. 希望这有助于某人。

I had the same problem, but simply adding array to permit was not enough. 我有同样的问题,但只是添加数组到许可是不够的。 I had to add type, too. 我也必须添加类型。 This way: 这条路:

params.require(:transaction).permit(:name, :tag_ids => [:id])

I am not sure if this is perfect solution, but after that, the 'Unpermitted parameters' log disappeared. 我不确定这是否是完美的解决方案,但在此之后,“未经许可的参数”日志消失了。

I found hint for that solution from this excellent post: http://patshaughnessy.net/2014/6/16/a-rule-of-thumb-for-strong-parameters 我从这篇优秀文章中找到了解决方案的提示: http//patshaughnessy.net/2014/6/16/a-rule-of-thumb-for-strong-parameters

If there are multiple items and item_array inside parameters like this-如果像这样的参数中有多个 items 和 item_array -

Parameters {"item_1"=>"value 1", "item_2"=> {"key_1"=> "value A1", 
"key_2"=>["val B2", "val C3"]} }

There we have array inside item_2 .我们在item_2有数组。
That can be permit as below-这可以允许如下 -

params.permit(item_2: [:key_1, :key_2 => [] ])

Above saved my day, may be helpful for you too.以上拯救了我的一天,可能对你也有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM