没有使用 Rails 4.0.2 和强参数将 Array 类型隐式转换为 Integer [英] No implicit conversion of type Array to Integer with Rails 4.0.2 and Strong Parameters

查看:49
本文介绍了没有使用 Rails 4.0.2 和强参数将 Array 类型隐式转换为 Integer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用简单的 has_many/belongs_to 关联更新嵌套模型

I'm trying to update a nested model with a simple has_many / belongs_to association

我已经在控制器中设置了参数

I've setup parameters in the controller with

params.require(:survey).permit(:name, :questions[[:id, :content]])

但我得到没有将数组类型隐式转换为整数

控制台转储如下.从我读过的类似问题来看,问题可能是这个 :questions_attributes 是如何散列的 - 不知道这需要在哪里修复 - 感谢任何想法!

console dump below. From similar issues I've read the problem may be how this :questions_attributes is hashed - no idea where this needs to be fixed tho - any ideas appreciated!

谢谢

Started PATCH "/surveys/1" for 127.0.0.1 at 2014-01-04 15:53:31 +1100
Processing by SurveysController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"l5ANLS/y2Z+aB4xiJzEw+pF+j7V7LQk4THfU7mkTGX4=", "survey"=>{"name"=>"do u like cats?", "questions_attributes"=>{"0"=>{"content"=>"nope3", "id"=>"1"}, "1"=>{"content"=>"no way", "id"=>"2"}}}, "commit"=>"Update Survey", "id"=>"1"}
  Survey Load (0.3ms)  SELECT "surveys".* FROM "surveys" WHERE "surveys"."id" = $1 LIMIT 1  [["id", "1"]]
Completed 500 Internal Server Error in 2ms

TypeError (no implicit conversion of Array into Integer):
  app/controllers/surveys_controller.rb:72:in `[]'
  app/controllers/surveys_controller.rb:72:in `survey_params'
  app/controllers/surveys_controller.rb:44:in `block in update'
  app/controllers/surveys_controller.rb:43:in `update'

更新:这是当 :question_attributes 没有额外括号时发布的内容

Update: This is what is posted when :question_attributes does not have extra parentheses

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"l5ANLS/y2Z+aB4xiJzEw+pF+j7V7LQk4THfU7mkTGX4=",
 "survey"=>{"name"=>"do u like cats?",
 "questions_attributes"=>{"0"=>{"content"=>"nope2",
 "id"=>"1"},
 "1"=>{"content"=>"no way",
 "id"=>"2"}}},
 "commit"=>"Update Survey",
 "id"=>"1"}

推荐答案

只需将 :questions[[:id, :content]] 替换为

:questions_attributes => [:id, :content]

这篇关于没有使用 Rails 4.0.2 和强参数将 Array 类型隐式转换为 Integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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