本文介绍了Rails 3嵌套模型表单,使用accep_nested_attributes_for深入2级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的嵌套模型表单在第一层深处工作良好。但我的印象是,你可以使用accept_nested_attributes_for深入多层次。但是,当我尝试下面的代码时,图像属性被附加到顶级问题模型,并且在表单提交时中断了未知属性图像错误。



我可以使用表单数据手工完成插入操作,但如果Rails能够自动处理它,则会因为显而易见的原因而更好。



我做错了什么?我试着改变| af |在字段为:图片做为其自己的唯一名称,但它没有任何效果。



型号:

  class问题< ActiveRecord :: Base 
has_one:answer
accep_nested_attributes_for:answer
end

class答案< ActiveRecord :: Base
belongs_to:问题
has_one:图片
accep_nested_attributes_for:图片
结束

图片< ActiveRecord :: Base
belongs_to:answer
end

控制器:

  def new 
@question = Question.new
answer = @ question.build_answer
image = answer .build_image

@case_id = params [:id]

render:layout => 'application',:template => '/ questions / form'
end

def create
question_data = params [:question]
@question = Question.new(question_data)
if @ question.save
...
end

查看:

  = form_for @question,:html => {:multipart => true} do | f | 

= f.label:text,问题文本:
= f.text_area:text,:rows => 7

%br
%br

= f.fields_for:answer,do | af |
= af.label:body,Answer Text:
= af.text_area:body,:rows => 7

%br
%br

= f.fields_for:image do | af |
= af.label:title,Image Title:
= af.text_field:title

%br

= af.label:file ,Image File:
= af.file_field:file

%br

= af.label:caption,Image Caption:
= af.text_area:caption,:rows => 7

= hidden_​​field_tag(case_id,value = @case_id)

= f.submit


解决方案

我认为你已经将表单变量稍微混合起来了。它应该是:

  = form_for @question,:html => {:multipart => true} do | f | 

= f.label:text,问题文本:
= f.text_area:text,:rows => 7

%br
%br

= f.fields_for:answer,do | af |
= af.label:body,Answer Text:
= af.text_area:body,:rows => 7

%br
%br

= af.fields_for:image do | img_form |
= img_form.label:title,Image Title:
= img_form.text_field:title

%br

= img_form.label:file ,Image File:
= img_form.file_field:file

%br

= img_form.label:caption,Image Caption:
= img_form.text_area:caption,:rows => 7

= hidden_​​field_tag(case_id,value = @case_id)

= f.submit

注意 form_for ... do | f | spawns f.fields_for ... do | af | ,然后产生 af.fields_for ... do | img_form |



关键是第二个fields_for。它应该是 af.fields_for:image do | img_form | 而不是 f.fields_for:image do | img_form |

My nested model form is working great on the first level deep. But I was under the impression that you could go many levels deep using accepts_nested_attributes_for. But when I try the code below, the "Image" attributes are attached to the top level "Question" model and it breaks upon form submission with an unknown attribute "Image" error.

I could do the inserts all by hand using the form data but if Rails can handle it automatically, it would be better for obvious reasons.

What am I doing wrong? I tried changing |af| in the "fields for :image do" to its own unique name but it didn't have any effect.

Models:

class Question < ActiveRecord::Base
  has_one :answer
  accepts_nested_attributes_for :answer
end

class Answer < ActiveRecord::Base
  belongs_to :question
  has_one :image
  accepts_nested_attributes_for :image
end

class Image < ActiveRecord::Base
  belongs_to :answer
end

Controller:

def new
    @question = Question.new
    answer = @question.build_answer
    image = answer.build_image

    @case_id = params[:id]

    render :layout => 'application', :template => '/questions/form' 
end

def create
  question_data = params[:question]
  @question = Question.new(question_data)
  if @question.save
  ...
end

View:

= form_for @question, :html => {:multipart => true} do |f|

  = f.label :text, "Question Text:"
  = f.text_area :text, :rows => 7

  %br
  %br

  =f.fields_for :answer, do |af|
    = af.label :body, "Answer Text:"
    = af.text_area :body, :rows => 7

    %br
    %br

    = f.fields_for :image do |af|
      = af.label :title, "Image Title:"
      = af.text_field :title

      %br

      = af.label :file, "Image File:"
      = af.file_field :file

      %br

      = af.label :caption, "Image Caption:"
      = af.text_area :caption, :rows => 7

  = hidden_field_tag("case_id", value = @case_id)

  = f.submit
解决方案

I think you've got the form variables slightly mixed up. It should be:

= form_for @question, :html => {:multipart => true} do |f|

  = f.label :text, "Question Text:"
  = f.text_area :text, :rows => 7

  %br
  %br

  =f.fields_for :answer, do |af|
    = af.label :body, "Answer Text:"
    = af.text_area :body, :rows => 7

    %br
    %br

    = af.fields_for :image do |img_form|
      = img_form.label :title, "Image Title:"
      = img_form.text_field :title

      %br

      = img_form.label :file, "Image File:"
      = img_form.file_field :file

      %br

      = img_form.label :caption, "Image Caption:"
      = img_form.text_area :caption, :rows => 7

  = hidden_field_tag("case_id", value = @case_id)

  = f.submit

Notice how form_for ... do |f| spawns f.fields_for ... do |af|, which in turns spawns af.fields_for ... do |img_form|.

The key is the second fields_for. It should be af.fields_for :image do |img_form| rather than f.fields_for :image do |img_form|.

这篇关于Rails 3嵌套模型表单,使用accep_nested_attributes_for深入2级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:06