Access object value within accepts_nested_attributes_for form

ε祈祈猫儿з 提交于 2019-12-07 10:17:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!