Pages

Thursday, 29 January 2015

collection_check_boxes Rails Example

HTML code
<div style="width: 150px;height: 100px;overflow: scroll;">
 <%= check_box_tag "All" %>All<br/>   
 <%= f.collection_check_boxes  :category_ids, Category.all, :id, :category_name do |b| %>
    <span>
    <%= b.check_box(class: "check_box")%>
    <%= b.label %>
    </span>
 <% end %>
</div>
Javascript Code 
<script type="text/javascript">
  $(document).ready(function(){
    $('#All').click(function() {  
        if (this.checked) {
           $('.check_box').each(function() {
              this.checked = true;                       
           });
        } else {
           $('.checkbox').each(function() {
              this.checked = false;                       
           });
        }  
     });
   });
</script>