has_many nested form with a has_one nested form within it

别来无恙 提交于 2019-12-09 15:52:08

问题


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 %>

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?


回答1:


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.



来源:https://stackoverflow.com/questions/16342210/has-many-nested-form-with-a-has-one-nested-form-within-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!