Rails 4 中双嵌套模型的不允许参数 [英] Unpermitted parameters for double-nested models in Rails 4

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

问题描述

我是 Rails 的新手,并基于此构建了一些东西,但它需要一些小的更新才能与 Rails 4 的强大参数兼容:

I'm new to Rails and built something based on this, but it needed small updates to make it compatible with Rails 4's strong parameters:

http://railscasts.com/episodes/196-nested-model-form-part-1

我根据此处的类似帖子将调查、问题和答案的参数列入白名单:

I whitelisted the parameters for the survey, questions and answers based on a similar post here:

Rails 4 嵌套属性未允许参数

class Survey < ActiveRecord::Base
  has_many :questions, :dependent => :destroy
  accepts_nested_attributes_for :questions, allow_destroy: true
end

class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :answers, :dependent => :destroy
  accepts_nested_attributes_for :answers, allow_destroy: true
end

class Answer < ActiveRecord::Base
  belongs_to :question
end

class SurveysController < ApplicationController
  def survey_params
    params.require(:survey).permit(:name, questions_attributes: [:id, :survey_id, :content])
  end

class QuestionsController < ApplicationController
  def question_params
    params.require(:question).permit(:survey_id, :content, answers_attributes: [:id, :question_id, :content])
  end

class AnswersController < ApplicationController
  def answer_params
    params.require(:answer).permit(:question_id, :content)
  end

第一个嵌套模型 (Question) 有效,但当我提交主调查表时,第二个 (Answer) 返回错误:

The first nested model (Question) works, but the second (Answer) returns an error when I submit the main survey form:

不允许的参数:answers_attributes

Unpermitted parameters: answers_attributes

Started POST "/surveys" for 127.0.0.1 at 2013-07-10 19:20:00 +0800
Processing by SurveysController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"pCK7j73kJPmld6gMXtbnBcheHU3pb9FGdjbHJPX6leE=", "survey"=>{"name"=>"test", "questions_attributes"=>{"0"=>{"content"=>"bbb", "answers_attributes"=>{"0"=>{"content"=>"bbbb"}}}}}, "commit"=>"Create Survey"}
Unpermitted parameters: answers_attributes

我检查了数据库,没有数据,在日志中发现错误.第一组嵌套数据(问题)在那里并且有效,只有第二组没有.我也有人们说你需要的 :id .

I checked the database and the data isn't there, and found the error in the log. The first set of nested data (the questions) are there and working, it's only the second next that isn't. I also have the :id in there that people say you need too.

据我所知,每个父级都需要将其将修改的直接嵌套属性列入白名单.我使用了完全相同的代码来解决问题,但即使我在问题中这样做了,答案也没有被列入白名单.

As far as I know, each parent needs to whitelist the direct nested attribute it will modify. I used exactly the same code to get questions to work, but answers aren't being whitelisted even though I have done so in questions.

任何指针表示赞赏.我似乎找不到任何可查看的双嵌套示例.

Any pointers appreciated. I can't seem to find any double-nested examples to look at.

更新:我通过反复试验解决了这个问题.

我发现解决方法是白名单需要匹配属性的嵌套.所以为了解决上面的问题,我改变了这个:

I found out the fix is that the whitelist needs to match the nesting of attributes. So to fix the above I changed this:

class SurveysController < ApplicationController
  def survey_params
    params.require(:survey).permit(:name, questions_attributes: [:id, :survey_id,    :content])
  end

到此:

class SurveysController < ApplicationController
  def survey_params
    params.require(:survey).permit(:name, questions_attributes: [:id, :survey_id,    :content, answers_attributes: [:id, :question_id, :content]])
  end

例如只需复制 answers_attributes 的白名单并将其插入到 questions_attributes 的结束]"之前.

E.g. simply copy the whitelist of the answers_attributes and insert it inside before the closing "]" for the questions_attributes.

推荐答案

我发现解决方法是白名单需要匹配属性的嵌套.所以为了解决上面的问题,我改变了这个:

I found out the fix is that the whitelist needs to match the nesting of attributes. So to fix the above I changed this:

class SurveysController < ApplicationController
  def survey_params
    params.require(:survey).permit(:name, questions_attributes: [:id, :survey_id,    :content])
  end

到此:

class SurveysController < ApplicationController
  def survey_params
    params.require(:survey).permit(:name, questions_attributes: [:id, :survey_id,    :content, answers_attributes: [:id, :question_id, :content]])
  end

例如只需复制 answers_attributes 的白名单并将其插入到 questions_attributes 的结束]"之前.

E.g. simply copy the whitelist of the answers_attributes and insert it inside before the closing "]" for the questions_attributes.

希望这能帮助其他有同样问题的人.

Hopefully this will help others with the same problem.

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

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