has_many嵌套形式,其中具有has_one嵌套形式 [英] has_many nested form with a has_one nested form within it

查看:55
本文介绍了has_many嵌套形式,其中具有has_one嵌套形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为模型创建表格,该表格具有动态数量的嵌套模型.我正在使用嵌套表单(如 RailsCasts 197 中所述) .为了使事情变得更加复杂,我的每个嵌套模型都与第三个模型建立了has_one关联,我也希望将其添加到表单中.

I am currently trying to make a form for a model, which has a dynamic number of nested models. I'm using Nested Forms (as described in RailsCasts 197). To make things even more complicated, each of my nested models has a has_one association with a third model, which I would also like to be added to the form.

对于任何对过度规范化或不正确方法感到疑惑的人,此示例都是我所面临问题的简化版本.实际上,事情要稍微复杂一些,这就是我们决定采用的方法.

For any who are wondering about over normalization or an improper approach, this example is a simplified version of the problem I'm facing. In reality, things are slightly more complex, and this is the approach we've decided to take.

一些示例代码来说明以下问题:

Some example code to illustrate the problem below:

#MODELS
class Test 
  attr_accessible :test_name, :test_description, :questions_attributes
  has_many :questions
  accepts_nested_attributes_for :questions
end

class Question
  attr_accessible :question, :answer_attributes
  belongs_to :test
  has_one :answer
  accepts_nested_attributes_for :answer
end

class Answer
  attr_accessible :answer
  belongs_to :question
end

#CONTROLLER
class TestsController < ApplicationController

  #GET /tests/new
  def new
    @test = Test.new
    @questions = @test.questions.build
    @answers = @questions.build_answer
  end

end

#VIEW
<%= form_for @test do |f| %>
  <%= f.label :test_name %>
  <%= f.text_box :test_name %>
  <%= f.label :test_description %>
  <%= f.text_area :test_description %>
  <%= f.fields_for :questions do |questions_builder| %>
    <%= questions_builder.label :question %>
    <%= questions_builder.text_box :question %>
      <%= questions_builder.fields_for :answer do |answers_builder| %>
        <%= answers_builder.label :answer %>
        <%= answers_builder.text_box :answer %>
      <% end %>
  <% end %>
  <%= link_to_add_fields 'New', f, :questions %>
<% end %>

此代码示例完全适用于Question的第一个实例.动态添加要创建的另一个问题时,就会发生此问题;不显示答案字段.我相信这是因为它们仅针对控制器中的第一个问题而构建.有没有一种方法可以使用nested_attributes来实现?

This code example works fully for the first instance of Question. The issue occurs when another question is dynamically added to be created; the answer fields are not displayed. I believe this is because they are only built for the first question in the controller. Is there a way to achieve this using nested_attributes?

推荐答案

我在这里解决了自己的问题.我所做的是,不是在控制器中构建答案模型(当您不知道视图中将要提出多少个问题时,这是不可能的),而是在调用fields_for:

I solved my own issue here. What I did was, instead of building the answer model in the controller (which is impossible when you do not know how many questions are going to be made in the view), I built it when calling fields_for:

#CONTROLLER
class TestsController < ApplicationController

  #GET /tests/new
  def new
    @test = Test.new
    @questions = @test.questions.build
  end

end

#VIEW
<%= form_for @test do |f| %>
  <%= f.label :test_name %>
  <%= f.text_box :test_name %>
  <%= f.label :test_description %>
  <%= f.text_area :test_description %>
  <%= f.fields_for :questions do |questions_builder| %>
    <%= questions_builder.label :question %>
    <%= questions_builder.text_box :question %>
    <%= questions_builder.fields_for :answer, @questions.build_answer do |answers_builder| %>
      <%= answers_builder.label :answer %>
      <%= answers_builder.text_box :answer %>
    <% end %>
  <% end %>
  <%= link_to_add_fields 'New', f, :questions %>
<% end %>

之所以行之有效,是因为无论在视图上构建多少个问题形式,都会针对正在构建的问题构建新的答案.

This works because no matter how many question forms are being built on the view, a new answer specific to the question being built is built.

这篇关于has_many嵌套形式,其中具有has_one嵌套形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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