我试着写一个路径来捕捉你的普通博客中帖子和评论之间的一对多关系
class Post < ActiveRecord::Base
has_many :comments
end
接着是一个注释.rb(在所有其他数据库设置中,包括post_id:integer供评论)
class Comment < ActiveRecord::Base
belongs_to :post
end
在我尝试使用的路线上
resources :posts do
resources :comments
end
但是我一点运气都没有-有Rails3专家的帮助吗?
当我通过这个url点击post控制器的“show”操作时
http://localhost:3000/posts/3
我遇到路由错误
No route matches {:controller=>"comments", :action=>"create"}
<% form_for Comment.new do |f| %>
<p>
<%= f.label :body, "new comment" %><br>
<%= f.text_area :body %>
</p>
<p><%= f.submit "add comment" %></p>
<% end %>
我是否需要更改我的表单,因为在更改路由之前,当我做一个简单的视图源代码时,操作指向/comments/{id}
编辑#2
我注意到我把路线改成这样之后
resources :comments
resources :posts
resources :posts do
resources :comments
end
除了我创建的注释不知道post\u id(在MySQL中,注释是持久化的,但它不知道它所属的post\u)之外,我的一切都正常工作
这可能是我的表格吗?