0

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

1 回答 1

3

我相信你会需要

accepts_nested_attributes_for :reply_fields

在模型中。

于 2013-09-25T14:21:44.183 回答