具有值数组的属性的未允许参数

时间:2015-02-09 09:28:43

标签: ruby-on-rails ruby-on-rails-4

这是Node#update的参数:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"F1HGLeI9E=", "node"=>{"name"=>"Lesty", "parents"=>["13", "35", ""], "family_tree_id"=>"2"}, "commit"=>"Update Node", "id"=>"38"}

这是我的NodesController

private

def node_params
  params.require(:node).permit(:user_id, :family_tree_id, :name, :description, :parent_id, :parent, :parents)
end

但是当我尝试更新产生上述日志的@node对象时,我收到此错误:

Node Load (1.1ms)  SELECT  "nodes".* FROM "nodes"  WHERE "nodes"."id" = $1 LIMIT 1  [["id", 38]]
Unpermitted parameters: parents
   (1.7ms)  BEGIN
   (1.3ms)  COMMIT
Redirected to http://localhost:3000/nodes/38
Completed 302 Found in 21ms (ActiveRecord: 4.0ms)

请务必注意,parents对象的@node属性是通过gem ancestry继承的,而不是直接在Node模型上或通过关联继承的。{ / p>

思想?

2 个答案:

答案 0 :(得分:2)

如果我没记错的话,你需要告诉strong_parameters预期数组。

def node_params
  params.require(:node).permit(:user_id, :family_tree_id, :name, :description, :parent_id, :parent, parents: [])
end

答案 1 :(得分:1)

你可以试试这个

 private

def node_params
  params.require(:node).permit(:name, :family_tree_id, parents: [])
end