【问题标题】:undefined method `post_comment_path' during step 9 of Ruby on Rails tutorialRuby on Rails 教程第 9 步中未定义的方法“post_comment_path”
【发布时间】:2012-03-07 02:16:19
【问题描述】:

[编辑:这个问题的解决方案在 routes.rb 文件中。我在 ":cmets" 行上有 "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

【问题讨论】:

  • 你应该得到行号和文件以伴随错误。这会很有帮助。
  • 我将输出添加到我的问题中。如果还有什么有用的,请告诉我(特定文件等)。
  • 试试这个帖子stackoverflow.com/questions/3774916/…的解决方案
  • 我的 routes.rb 文件包含该解决方案中的相应行,假设我正在正确阅读解决方案。一些构造仍然是外星人。事实上,一个多小时前我读过那篇文章:)
  • 你能发布你的路线和模型吗?

标签: ruby-on-rails-3 methods undefined


【解决方案1】:

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

resources :posts do
  resources :comments
end

【讨论】:

  • 修复了它。谢谢!回顾教程,他们说得对。我想我一定是打错了……
  • 常见错误,我自己做过几次 :) 而且有时很难找到,因为它是有效的路由。
  • 有趣的是——我在 Stackoverflow 上的最后一个问题是由于 Java 中的 Collections 末尾缺少“s”...
  • 也为我解决了,没有写成复数,2年后一定要运行同样的教程!
猜你喜欢
  • 1970-01-01
  • 2015-11-13
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多