vendredi 4 mars 2016

Rails check_box form to create has_many relationship

I have a model called account which has_many :options. I want to create a form in which i can list all the options with a checkbox at the side, so the current account can select the options he/she wants inside a form so I can create the has_many relation.

This is what i have

def index
    @account = current_account
    @options = ['Op 1', 'Op 2', 'Op 3', 'Op 4']
end

and for the view:

 <%= form_for(@account, url: options_path) do |f| %>
    <% @options.each do |op| %>
      <div class="checkbox">
         <%= f.check_box(?????, {:multiple => true}, op, nil) %>
      </div>
    <% end %>
    <%= f.submit class: 'btn btn-default' %>
  <% end %>

This is obviously not working and I'm pretty sure this is not the right way to achieve what I want to do, so any help would be appreciated.




Aucun commentaire:

Enregistrer un commentaire