问题
I have the following setup:
class Option < ActiveRecord::Base
has_many :size_prices
accepts_nested_attributes_for :size_prices
end
def new
@option = Option.new
@sizes = @customization.item.sizes
@sizes.each do |size|
@option.size_prices.build({:size_id => size.id})
end
end
<%= f.fields_for :size_prices do |price_form| %>
I would like to do something like:
<%= Size.find(price_form.size_id).name %>
<%= price_form.text_field :amount %>
<% end %>
Is there any way to access the size_id's of each object with the form? I would like to get the size objects name.
回答1:
Yep, the .object on fields_for will give you the object it is building for
<%= f.fields_for :size_prices do |price_form| %>
<%= price_form.object.size_id %>
...
来源:https://stackoverflow.com/questions/6181873/access-object-value-within-accepts-nested-attributes-for-form