简体   繁体   中英

formtastic - array of objects with has_many in the form field

I have a new action in the marks controller. When the user clicks on 'new marks', I need to display a form which contains list of papers for that student. And each paper has a list of options which the user needs to select.

The association between the models are:

Mark.rb

 belongs_to :paper


 paper.rb

 has_many :options

 Option.rb

  belongs_to :paper

In the form for @mark, I need to display all the papers and the list of options using 'formtastic'.

I tried doing,

   <% @array_papers.each do |paper| %>
    <% options = paper.options %>

   <%= semantic_form_for paper, url:thinking_marks_path(student_id: @student.id) do |form| %>
        <li class="each-question">
          <%= form.input :paper, label: "{paper[:name]}" %>
          <%= semantic_fields_for :options, paper.object.options do |option|  %>
           <%= option.input :option, as: :check_boxes %>
          <% end %>


        </li>
    <% end %>

    </ul>

    <p> <%= link_to 'Save',thinking_marks_path( student_id: @student.id ),  :class => 'simple-button course-type' %>
    </p>

<% end %>

But it is throwing error:

undefined method `option' for #<Paper:0x0000000fe6d3e0>

What should I do ?

The form variable is the form of paper object.

I advise you to better naming your form variables, eg. paper_form and option_form .

This code should work:

  <%= semantic_fields_for :options, paper.object.options do |option_form|  %>
    <%= option_form.input :option, as: :check_boxes %>
  <% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM