在accepts_nested_attributes_for表单中访问对象值

时间:2011-05-31 00:36:16

标签: ruby-on-rails nested-forms

我有以下设置:

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

有没有办法用表格访问每个对象的size_id?我想获取大小对象名称。

1 个答案:

答案 0 :(得分:3)

是的,fields_for上的.object将为你提供它正在构建的对象

<%= f.fields_for :size_prices do |price_form| %>
  <%= price_form.object.size_id %>
  ...