我的评论嵌套在嵌入游戏中的帖子中。我不能为我的生活弄清楚如何添加评论。我得到一个未定义的方法。
Showing /Users/***/Documents/TestRoutes/app/views/posts/show.html.erb where line #31 raised:
undefined method `post_comments_path' for #<#<Class:0x007fb2159834a0>:0x007fb215980228>
Extracted source (around line #31):
28: <% end %>
29:
30: <h2>Add a comment:</h2>
31: <%= form_for([@post, @post.comments.build]) do |f| %>
32: <div class="field">
33: <%= f.label :commenter %><br />
34: <%= f.text_field :commenter %>
<%= form_for([@post, @post.comments.build]) do |f| %>
resources :games do
resources :posts do
resources :comments
end
end
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
redirect_to game_post_path(@post)
end
class Comment < ActiveRecord::Base
belongs_to :post
attr_accessible :body, :commenter
end
class Post < ActiveRecord::Base
attr_accessible :post, :title, :game_id, :user_id
belongs_to :user
belongs_to :game
has_many :comments
答案 0 :(得分:1)
是完整的帖子/评论路线吗?
resources :games do
resources :posts do
resources :comments
end
end
如果是这样,你也应该指定游戏,比如
<%= form_for([@game, @post, @post.comments.build]) do |f| %>
重定向行
相同redirect_to game_post_path(@post)
没有游戏,很可能会引发错误