2
我发现了大量有关如何构建多模型窗体和多模型显示的示例。但是如果我想要分开形式和显示呢?在单独的窗体/视图中导航has_many嵌套属性
post.rb:
class Post < ActiveRecord::Bas
has_many :comments, dependent: :destroy
attr_accessible :comments_attributes
accepts_nested_attributes_for :comments
end
comment.rb:
class Comment < ActiveRecord::Base
belongs_to :post
end
posts_controller.rb:
def new
@post = Post.new
@post.comments.build
...
end
routes.db:
resources posts do
resources comments
end
我有一个链接后发表评论指数在我的帖子指数:
的意见/职位/ index.html.erb:
...
<%= link_to 'Comments', post_comments_path(post) %>
...
邮政和评论每个都产生自己的脚手架形式(不嵌套) 。
<%= form_for(@post) do |f| %>
...
<%= form_for(@comment) do |f| %>
...
在评论指数我遍历发表评论:
的意见/评论/ index.html.erb:
<% @post = Post.find(params[:post_id]) %> //works fine
<% @post.comments.each do |comment| %>
...
<% end %>
然而,添加一个新的注释(具体职位ID后下)该表中的帖子评论索引是空!
请帮忙。 谢谢:)
我在索引视图中看到此错误... 找不到没有ID –
roadev