Rails 4 不允许的数组参数 [英] Rails 4 Unpermitted Parameters for Array

查看:40
本文介绍了Rails 4 不允许的数组参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型中有一个数组字段,我正在尝试更新它.

I have an array field in my model and I'm attempting to update it.

我的强参数方法如下

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

传入的参数是

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

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

我想这与属性 categories 是一个数组这一事实有关,因为其他一切看起来都很好.再说一次,我可能是错的.那么,我的代码有什么问题,为什么在明确允许的情况下不让我保存类别字段?谢谢.

I want to think it has something to do with the fact that the attribute categories is 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.

推荐答案

试试这个

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

(忽略我的评论,我认为这不重要)

(Disregard my comment, I don't think that matters)

这篇关于Rails 4 不允许的数组参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆