ruby on rails - has_many nested form with a has_one nested form within it -


i trying make form model, has dynamic number of nested models. i'm using nested forms (as described in railscasts 197). make things more complicated, each of nested models has has_one association third model, added form.

for wondering on normalization or improper approach, example simplified version of problem i'm facing. in reality, things more complex, , approach we've decided take.

some example code illustrate 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 |f| %>   <%= f.label :test_name %>   <%= f.text_box :test_name %>   <%= f.label :test_description %>   <%= f.text_area :test_description %>   <%= f.fields_for :questions |questions_builder| %>     <%= questions_builder.label :question %>     <%= questions_builder.text_box :question %>       <%= questions_builder.fields_for :answer |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 first instance of question. issue occurs when question dynamically added created; answer fields not displayed. believe because built first question in controller. there way achieve using nested_attributes?

i solved own issue here. did was, instead of building answer model in controller (which impossible when not know how many questions going made in view), built 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 |f| %>   <%= f.label :test_name %>   <%= f.text_box :test_name %>   <%= f.label :test_description %>   <%= f.text_area :test_description %>   <%= f.fields_for :questions |questions_builder| %>     <%= questions_builder.label :question %>     <%= questions_builder.text_box :question %>     <%= questions_builder.fields_for :answer, @questions.build_answer |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 being built on view, new answer specific question being built built.


Comments

Popular posts from this blog

Eclipse formatter for java ending braces -

ruby - Nesting modules inside of a Rails eninge gem -

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -