Created
June 15, 2011 10:04
-
-
Save christineyen/1026833 to your computer and use it in GitHub Desktop.
Nested form for a serialized has_many relationship
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddBillItemsToUsers < ActiveRecord::Migration | |
def self.up | |
add_column :users, :bill_items, :string | |
end | |
def self.down | |
remove_column :users, :bill_items | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BillItem | |
attr_accessor :line_item, :subtotal | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= form_for(@user) do |f| | |
= f.fields_for :bill_items do |bill_f| | |
= f.text_field :line_item | |
= f.text_field :subtotal | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
serialize :bill_items, Array | |
... | |
def bill_items_attributes=(attributes) | |
# do something | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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