Topic: undefined local variable or method 'posts_path'

I was trying to follow the tutorial instructions on http://guides.rubyonrails.org/getting_started.html Everything went fine til I came to section

"6.2 Adding a Link
To hook the posts up to the home page you’ve already created, you can add a link to the home page. Open app/views/home/index.html.erb and modify it as follows:
(h1)Hello, Rails!(/h1)
(%= link_to "My Blog", posts_path %)
The link_to method is one of Rails’ built-in view helpers. It creates a hyperlink based on text to display and where to go – in this case, to the path for posts."

(angle brackets replaced by normal brackets for the sake of the forum)

The result I get looks as follows:
"NameError in Home#index
Showing c:/Users/paulousek/My Documents/Aptana Studio 3 Workspace/TestAptanaRails/blog/app/views/home/index.html.erb where line #2 raised:
undefined local variable or method `posts_path' for #<#<Class:0x379c800>:0x2f5c080>
Extracted source (around line #2):
1: (h1)Home#index(/h1)
2: (%= link_to "My Blog", posts_path %)"

So what is missing or wrong here??

Last edited by PPPaul (2012-10-09 13:11:06)

Re: undefined local variable or method 'posts_path'

 <%= link_to "My Blog", posts_path %>

posts_path is a method that should be defined in your routes
a RESTful route is normally defined automatically when you scaffold a model and should be there if you scaffolded the posts model

Did you completwe step 6

6 Creating a Resource

In the case of the blog application, you can start by generating a scaffold for the Post resource: this will represent a single blog posting. To do this, enter this command in your terminal:
$ rails generate scaffold Post name:string title:string content:text

This will have created the posts path route for you automatically.
If you have done that then It may be that you need to restart your server for Rails to recognise the changes. But for whatever reason you don;t have a post resource in your routes.rb.
It should look something like this

resources :posts

Your issue could also be indicative of not having completed step 6 correctly (A typo or maybe you called your resource somethiong other than post.)
If you need further assistance can you post the contents of your routes.rb file and details what scaffolding you have created.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: undefined local variable or method 'posts_path'

@jamesw
Thanks for your helpful reply! If you're right I should try to continue with the step in question, i.e. #6.
Maybe I was mislead by the assumption that the tutorial could be followed in a step by step manner without any skips or jumps. And up to the point 6.2 that did indeed work...

Re: undefined local variable or method 'posts_path'

Now you have confused me.

If you managed to get to step 6.2 without doing step 6 then you have missed a step! Yes The tutorial should be followed step by step and it will work.
Did you run that scaffold command?

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: undefined local variable or method 'posts_path'

@jamesw,
sorry for confusing you, but yes, I did not skip a step and did step 6 including the scaffolding.
Scaffolding did create/change my routes.rb file, so that it now contains this (comment-lines omitted):

====

Blog::Application.routes.draw do
  get "home/index"


  root :to => "home#index"

 
end

====

It obviously does not contain the line you expected:
"resources :posts" ...

Re: undefined local variable or method 'posts_path'

Scaffolding did create/change my routes.rb file

Pehaps you ran the scaffold in the wrong folder? according to the contents of the routes.rb file you posted, the routes.rb file did not change.

anyway, for whateer reason, the scaffold from step 6 did not work.
You can safely rerun a scaffold so I would suggest you repeat step 6.

As an interesting exercise, you could check to see if you have any controllers other than your home controller, and see if there are any view or model folders that refer to anything other than home. This would indicate that the scaffold partially worked.

So just run

rails generate scaffold Post name:string title:string content:text

, making sure that you are in the correct project folder when you run it.
Check the output that is generated from that line to make sure all went well and then all should be good.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)