Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `post_comment_path' during step 9 of Ruby on Rails tutorial

[Edit: The solution to this problem is in the routes.rb file. I have "resource" instead of "resources" on the ":comments" line]

I'm following the guide at http://guides.rubyonrails.org/getting_started.html, and I'm at section 9 "Deleting Comments". I've been following the guide step-by-step, and have been cutting/pasting the code instead of typing it, so I doubt I have a typo - more like I missed a step. I'd like to fix this before continuing with the tutorial as I'm brand-spanking-new to Rails and it's still pretty foreign to me.

Any idea what's wrong?

An except from my routes.rb:

  resources :posts do
    resource :comments
  end

When I attempt to view a post with a comment, I receive the following error:

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
like image 326
Steve Avatar asked Dec 27 '22 04:12

Steve


1 Answers

In your routes, the routing to comments should be plural as well, meaning resources instead of resource. Try to do it like this:

resources :posts do
  resources :comments
end
like image 167
DanneManne Avatar answered May 20 '23 08:05

DanneManne