我有一些奇怪的事情,在我的视图中我的rails 4应用程序:
<%= f.object.reply_fields.inspect %>
# prints out 4 objects
<%= f.fields_for :reply_fields do |r| %>
<%= r.object.inspect %>
<% end %>
# prints out "nil" only once
我不明白这里发生了什么,我该怎么做才能循环4个对象?更多信息:
# routes
resources :invites do
resources :replies
end
# Invite model
has_many :inputs
has_many :replies
# Reply model
belongs_to :invite
has_many :reply_fields
# ReplyField model
belongs_to :reply
belongs_to :input
# Input model
belongs_to :invite
has_many :reply_fields
# controller
def new
@reply = Reply.new(invite: @invite)
@invite.inputs.each do |i| # there are 4 items in there
@reply.reply_fields.build(input:i)
end
end
# viev
<%= form_for [@invite, @reply] do |f| %>
...
答案 0 :(得分:3)
我相信你需要
accepts_nested_attributes_for :reply_fields
在模型中。