Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is this "posts_path" variable defined?

I'm following this tutorial (seems good) for Rails. After I run

ruby script/generate scaffold Post

then this link works in one of the erb files:

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

WHY? I've looked for "posts_path" in the whole app and it's nowhere to be found. On the other hand, this

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

does not work, and it's also a Controller.

Where is the posts_path defined?

like image 387
Dan Rosenstark Avatar asked Dec 05 '22 07:12

Dan Rosenstark


1 Answers

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

map.resources :posts

See the API docs for information on what other named routes you get for free.

Also you can run rake routes and see what all your routes.rb is giving you.

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

map.home '/home', :controller => "home", :action => "index"
like image 100
Otto Avatar answered Dec 17 '22 12:12

Otto