[编辑:此问题的解决方案在 routes.rb 文件中。我在 ":comments"行上有 "resource"而不是 "resources"]

我正在关注 http://guides.rubyonrails.org/getting_started.html 的指南,我在第 9 节“删除评论”。我一直在一步一步地遵循指南,并且一直在剪切/粘贴代码而不是输入它,所以我怀疑我有一个错字 - 更像是我错过了一个步骤。我想在继续本教程之前解决这个问题,因为我是 Rails 的新手,它对我来说仍然很陌生。

知道出了什么问题吗?

除了我的routes.rb:

  resources :posts do
    resource :comments
  end

当我尝试查看带有评论的帖子时,我收到以下错误:
NoMethodError in Posts#show

Showing C:/Documents and Settings/stevez/Desktop/blog/app/views/comments/_comment.html.erb where line #12 raised:

undefined method `post_comment_path' for #<#<Class:0x1f13238>:0x1eecf70>
Extracted source (around line #12):

9: </p>
10:
11: <p>
12:   <%= link_to 'Destroy Comment', [comment.post, comment],
13:                :confirm => 'Are you sure?',
14:                :method => :delete %>
15: </p>
Trace of template inclusion: app/views/posts/show.html.erb

Rails.root: C:/Documents and Settings/stevez/Desktop/blog

Application Trace | Framework Trace | Full Trace
app/views/comments/_comment.html.erb:12:in `_app_views_comments__comment_html_erb___131033543_18407100'
app/views/posts/show.html.erb:19:in `_app_views_posts_show_html_erb___843714715_14332200'
app/controllers/posts_controller.rb:18:in `show'
Request

Parameters:

{"id"=>"2"}
Show session dump

Show env dump

GATEWAY_INTERFACE: "CGI/1.1"
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
HTTP_ACCEPT_CHARSET: "ISO-8859-1,utf-8;q=0.7,*;q=0.3"
HTTP_ACCEPT_ENCODING: "gzip,deflate,sdch"
HTTP_ACCEPT_LANGUAGE: "en-US,en;q=0.8"
HTTP_CACHE_CONTROL: "max-age=0"
REMOTE_ADDR: "127.0.0.1"
REMOTE_HOST: "localhost"
SERVER_NAME: "localhost"
SERVER_PROTOCOL: "HTTP/1.1"
Response

Headers:

None

最佳答案

在你的路由中,注释的路由也应该是复数,意思是 resources 而不是 resource 。尝试这样做:

resources :posts do
  resources :comments
end

关于ruby-on-rails-3 - Ruby on Rails 教程第 9 步中未定义的方法 `post_comment_path',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9594900/

10-14 03:21