Skip to content

Instantly share code, notes, and snippets.

@christineyen
Created June 15, 2011 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save christineyen/1026833 to your computer and use it in GitHub Desktop.
Save christineyen/1026833 to your computer and use it in GitHub Desktop.
Nested form for a serialized has_many relationship
class AddBillItemsToUsers < ActiveRecord::Migration
def self.up
add_column :users, :bill_items, :string
end
def self.down
remove_column :users, :bill_items
end
end
<form action="/users/1" method="post">
<input id="user_bill_items_line_item" name="user[bill_items][line_item]" type="text" />
<input id="user_bill_items_subtotal" name="user[bill_items][subtotal]" type="text" />
</form>
class BillItem
attr_accessor :line_item, :subtotal
end
= form_for(@user) do |f|
= f.fields_for :bill_items do |bill_f|
= f.text_field :line_item
= f.text_field :subtotal
...
<form action="/users/1" method="post">
<input id="user_bill_items_attributes_0_line_item" name="user[bill_items_attributes][0][line_item]" type="text" value="Walgreens - Garden Hose" />
<input id="user_bill_items_attributes_0_subtotal" name="user[bill_items_attributes][0][subtotal]" type="text" value="11.0" />
</form>
class User < ActiveRecord::Base
serialize :bill_items, Array
...
def bill_items_attributes=(attributes)
# do something
end
end
class User < ActiveRecord::Base
serialize :bill_items, Array
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment