문제

I have a form to build several objects following Ryan Bates’ nested form tutorials (#196 and #197). My form looks like this:

<%=form_for @group_poll, :remote => true do |f|%>
   <%=f.fields_for :questions do |builder| %>
      <%=render "group_polls/question_fields",  :f => builder%>    
   <%end%>
   <p><%= link_to_add_group_question "#{t(:addquestion)}", f, :questions%></p>
<%end%>

The partial looks like:

<div class="row-fluid">
    <div class="span3"><%=f.label "#{t(:question)}:"%></div>
    <div class="span3"><%=f.text_field :admin_question, :required => true%></div>
    <div class="span1"><%=f.submit "#{t(:send)}", :name => "send_#{f.object.id}", :class=>"btn-white"%></div>
    <div class="span1"><%=f.submit "#{t(:save)}", :name => "save_#{f.object.id}", :class=>"btn-white"%></div>
</div>

I want to preload some questions that exist in the DB when accessing the page in the app and also have the chance to add new questions. In the controller action I have @group_poll.questions.build() for each poll. It works OK for generating the questions' fields, but they are empty. I don’t know how to preload those polls with the data in DB. I tried passing the question object @group_poll.questions.build(question), but I had no luck. I followed this SO question’s answer, but I get lost.

Is it posible what I want to do?

도움이 되었습니까?

해결책

if you want to preload existing questions in the DB, you shouldn't use build() because it builds and empty object.

If you take build() out, does it work?

@group_poll.questions
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top