Ruby on rails 为什么我的嵌套表单根本没有出现?

Ruby on rails 为什么我的嵌套表单根本没有出现?,ruby-on-rails,forms,ruby-on-rails-4,nested-forms,Ruby On Rails,Forms,Ruby On Rails 4,Nested Forms,这就是我所拥有的: post.rb: class Post < ActiveRecord::Base has_many :replies, :dependent => :destroy accepts_nested_attributes_for :replies, :allow_destroy => true end 回复部分中的输出仅为: <div class="replies"> </div> 回复字段根本不显示。可能是什么问题?

这就是我所拥有的:

post.rb:

class Post < ActiveRecord::Base
  has_many :replies, :dependent => :destroy
  accepts_nested_attributes_for :replies, :allow_destroy => true
end
回复部分中的输出仅为:

<div class="replies">
  </div>

回复字段根本不显示。可能是什么问题?


  <div class="replies">
    <%= f.fields_for :replies do |builder| %>
      <%= render 'reply_fields', :f => builder %>
    <% end %>
  </div>
建筑商%>
除了注释和答案,您还需要
构建
回复
ActiveRecord对象:

#app/controllers/posts_controller.rb
def new
    @post = Post.new
    @post.replies.build 
end

我想你可能错过了一个等号:
谢谢,但奇怪的是,每次我编辑帖子时都会添加一个新的回复。即使应答控制器中的编辑操作为空。您使用的是继承资源还是类似的资源?@Rick Peck in routes.rb?不,它们只是一个接一个地列出。您评论说,尽管您在编辑操作中没有任何代码,但您的表单在编辑帖子时会显示出来--我想知道这是否是因为您使用的是
继承的\u资源
(这将否定编辑中对代码的需要)?嗯,不是,在我的代码中没有任何说明继承的资源的内容。
  create_table "posts", force: true do |t|
    t.string   "content"
    t.string   "user"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "replies", force: true do |t|
    t.string   "content"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "post_id"
  end
<div class="replies">
  </div>
  <div class="replies">
    <%= f.fields_for :replies do |builder| %>
      <%= render 'reply_fields', :f => builder %>
    <% end %>
  </div>
#app/controllers/posts_controller.rb
def new
    @post = Post.new
    @post.replies.build 
end