我有一些奇怪的事情发生,在我的 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| %>
...