Ruby-on-rails Rails 4 不允许的数组参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17868746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 4 Unpermitted Parameters for Array
提问by jason328
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"=>"[email protected]", "categories"=>",2"}}
I want to think it has something to do with the fact that the attribute categoriesis an array since everything else looks fine. 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.
我想认为它与属性categories是一个数组这一事实有关,因为其他一切看起来都很好。再说一次,我可能是错的。那么,我的代码有什么问题,为什么在明确允许的情况下不让我保存类别字段?谢谢。
回答by Slicedpan
Try this
尝试这个
params.require(:post).permit(:name, :email, :categories => [])
(Disregard my comment, I don't think that matters)
(忽略我的评论,我认为这不重要)
回答by Imtiaz Emu
in rails 4, that would be,
在rails 4中,那将是,
params.require(:post).permit(:name, :email, {:categories => []})
回答by Mohammad Abu Musa
The permitted scalar types are String, Symbol, NilClass, Numeric, TrueClass, FalseClass, Date, Time, DateTime, StringIO, IO, ActionDispatch::Http::UploadedFileand Rack::Test::UploadedFile.
允许的标量类型为String, Symbol, NilClass, Numeric, TrueClass, FalseClass, Date, Time, DateTime, StringIO, IO,ActionDispatch::Http::UploadedFile和Rack::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 Githubsays:
params.require(:post).permit(:name, :email, :categories => [])
I hope this works out for you.
我希望这对你有用。
回答by Carlo S
I had the same problem but in my case I had also to change from:
我有同样的问题,但在我的情况下,我也不得不改变:
<input type="checkbox" name="photographer[attending]" value="Baku">
<input type="checkbox" name="photographer[attending]" value="Baku">
to:
到:
<input type="checkbox" name="photographer[attending][]" value="Baku">
<input type="checkbox" name="photographer[attending][]" value="Baku">
Hope this is helping someone.
希望这对某人有所帮助。
回答by Heikki Hannula
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

