jeudi 14 janvier 2016

Simple form - checkboxes

I have the models Search, Color and ColorsSearch:

def Search
   has_many :colors, :through => :colors_searches
end

def Color
   has_many :searches, :through => :colors_searhes
end

def ColorsSearch
   belongs_to :color
   belongs_to :search
end

In my view I want to have multiple color checboxes and when a user submit the form, a new search record is created with the colors_serches records (one for each colors selected).

 <% Color.with_products.order(:name).each do |color| %>
     <% if color.name != 'none' %>
         <div class="checkbox">
             <input type="checkbox" name="search[color_id]" value="<%= color.id %>" id="color-checkbox-<%= color.id %>">
             <label for="color-checkbox-<%= color.id %>" style="background-color: <%= color.hex %>;"></label>
         </div>
     <% end %>
 <% end %>

Two questions:

  1. How can I rewrite the following code in my view to achieve this using simple form ?
  2. How should the controller looks like?

Thank you for your ideas, Miroslav




Aucun commentaire:

Enregistrer un commentaire