Look at Ian's answer. This is definitely the right way, but ... I prefer not to use simple_form or formtastic for this example, so that it is as simple as possible.
I used the Iain HTML output to extract the HTML that I need. This snippet is the same as Iain's answer, without the need for an additional library:
<% Line.all.each do |line| %> <%= hidden_field_tag "invoice[line_ids][]" %> <%= check_box_tag "invoice[line_ids][]", line.id, @invoice.lines.include?(line), :id => "invoice_line_ids_#{line.id}" %> <% end %>
PS: Line.all and @invoice.lines... must be extracted to the controller and invoice model, they do not belong to the view. They are used only for brevity.
source share