0

我在嵌套在游戏中的帖子中嵌套了评论。我一生都无法弄清楚如何添加评论。我得到一个未定义的方法。

 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
4

1 回答 1

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)

没有游戏很可能会引发错误

于 2012-08-09T14:15:11.680 回答