TTDaVeTT
TTDaVeTT

Reputation: 127

Access object value within accepts_nested_attributes_for form

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.

Upvotes: 4

Views: 1821

Answers (1)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

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 %>
  ...

Upvotes: 3

Related Questions