Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 31st, 2012  |  syntax: None  |  size: 1.11 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using check boxes with a has_many relationship
  2. create_table "invoices" do |t|
  3. end
  4.  
  5. create_table "lines" do |t|
  6.   t.integer  "invoice_id"
  7. end
  8.        
  9. class Invoice < ActiveRecord::Base
  10.   has_many :lines
  11. end
  12.  
  13. class Line < ActiveRecord::Base
  14.   belongs_to :invoice
  15. end
  16.        
  17. <%= f.input :line_ids, :as => :check_boxes %>
  18.        
  19. <span>
  20.   <input name="invoice[line_ids][]" type="hidden" value="" />
  21.   <input checked="checked" class="check_boxes optional" id="invoice_line_ids_1" name="invoice[line_ids][]" type="checkbox" value="1" />
  22.   <label class="collection_check_boxes" for="invoice_line_ids_1">Line Name 1</label>
  23. </span>
  24.  
  25. <span>
  26.   <input name="invoice[line_ids][]" type="hidden" value="" />
  27.   <input checked="checked" class="check_boxes optional" id="invoice_line_ids_2" name="invoice[line_ids][]" type="checkbox" value="2" />
  28.   <label class="collection_check_boxes" for="invoice_line_ids_2">Line Name 2</label>
  29. </span>
  30.        
  31. <% Line.all.each do |line| %>
  32.   <%= hidden_field_tag "invoice[line_ids][]" %>
  33.   <%= check_box_tag "invoice[line_ids][]", line.id, @invoice.lines.include?(line), :id => "invoice_line_ids_#{line.id}" %>
  34. <% end %>