Ruby on rails Rails 4阵列的未经允许的参数
我的模型中有一个数组字段,我正在尝试更新它 我的强参数方法如下Ruby on rails Rails 4阵列的未经允许的参数,ruby-on-rails,ruby,ruby-on-rails-4,strong-parameters,Ruby On Rails,Ruby,Ruby On Rails 4,Strong Parameters,我的模型中有一个数组字段,我正在尝试更新它 我的强参数方法如下 def post_params params["post"]["categories"] = params["post"]["categories"].split(",") params.require(:post).permit(:name, :email, :categories) end 我在控制器中的操作如下 def update post = Post.find(params[:id] if post
def post_params
params["post"]["categories"] = params["post"]["categories"].split(",")
params.require(:post).permit(:name, :email, :categories)
end
我在控制器中的操作如下
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
然而,每当我提交更新帖子时,我都会在我的开发日志中看到
Unpermitted parameters: categories
传递的参数是
Parameters: {"utf8"=>"✓", "authenticity_token"=>"auth token", "id"=>"10",
"post"=>{"name"=>"Toni Mitchell", "email"=>"eileen_hansen@hayetokes.info", "categories"=>",2"}}
我想这与属性categories
是一个数组这一事实有关,因为其他一切看起来都很好。再说一次,我可能错了。那么,我的代码出了什么问题,为什么不让我保存categories字段呢?谢谢。试试这个
params.require(:post).permit(:name, :email, :categories => [])
(忽略我的评论,我不认为这有什么关系)允许的标量类型有
String
,Symbol
,NilClass
,Numeric
,TrueClass
,日期
,DateTime
,DateTime
,StringIO
,,ActionDispatch::Http::UploadedFile
和Rack::Test::UploadedFile
要声明params中的值必须是允许的标量值数组,请将键映射到空数组:
params.permit(:id => [])
这是政府所说的:
我希望这对您有用。在rails 4中
params.require(:post).permit(:name, :email, {:categories => []})
我也有同样的问题,但在我的情况下,我也不得不改变:
致:
希望这对某人有所帮助。我也有同样的问题,但仅仅向permit添加数组是不够的。我也必须添加类型。这样:
params.require(:transaction).permit(:name, :tag_ids => [:id])
我不确定这是否是完美的解决方案,但在那之后,“unpermittedparameters”日志消失了
我从这篇优秀的帖子中找到了解决方案的提示:这可能是由于您设置categories参数值的方式,iirc参数对象不是一个简单的散列,你试过一个更简单的例子吗?这对我来说很有效,但是如果我们已经在模型中定义了该属性的序列化,为什么它需要在一个单独的哈希中呢?嗨,我有一个问题Bid(#69846105528920)预期,get String(#8248680)嗨,有没有办法根据数组中的类别来查找post,例如post.find\u by(标签:'sports')
?还可以解释一下为什么@slicedpan会起作用
params.require(:transaction).permit(:name, :tag_ids => [:id])