在accepts_nested_attributes_for表单内的访问对象值 [英] Access object value within accepts_nested_attributes_for form
本文介绍了在accepts_nested_attributes_for表单内的访问对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下设置:
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?我想获取尺寸对象的名称.
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.
推荐答案
是的,fields_for上的.object将为您为其创建的对象
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 %>
...
这篇关于在accepts_nested_attributes_for表单内的访问对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文