Where is this "posts_path" variable defined?

I am following this tutorial (seems good) for Rails. After launch

ruby script/generate scaffold Post 

then this link works in one of the erb files:

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

WHY? I searched for "posts_path" throughout the application and it was not found anywhere. On the other hand, this

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

not working as well as the controller.

Where is posts_path indicated?

+4
source share
3 answers

posts_path is a named route that you get for free from a route that script/generate scaffold added. See routes.rb , you should see something like this:

 map.resources :posts 

Refer to the API docs for information on which other named routes you get for free.

You can also run rake routes and see what your routes.rb to offer.

If you want a home_path named route to add this line to your routes.rb :

 map.home '/home', :controller => "home", :action => "index" 
+14
source

I believe that posts_path is dynamically created by Rails at runtime. Look at your routes.rb file. The home page is probably not defined in the same way as the posts. This has nothing to do with your controllers, it depends on the route definition.

+1
source

map.root :controller => "home" will be a shorter way to write the path to your home directory. This will use / has a home, not / home. If you still want to use / home (and home_path), map.home 'home', :controller => "home" will do the same.

There's a great guide written by Mike Gunderla about everything you need to know about routing.

+1
source

All Articles