Where is this “posts_path” variable defined?-Collection of common programming errors
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:
WHY? I’ve looked for “posts_path” in the whole app and it’s nowhere to be found. On the other hand, this
does not work, and it’s also a Controller.
Where is the posts_path defined?
-
posts_pathis a named route you get for free from the route that was added byscript/generate scaffold. Seeroutes.rbyou should see something like this:map.resources :postsSee the API docs for information on what other named routes you get for free.
Also you can run
rake routesand see what all yourroutes.rbis 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" -
I believe that “posts_path” is created dynamically by Rails at runtime. Look at your routes.rb file – Home is probably not defined the same way as Posts. It has nothing to do with you controllers, it’s dependent on the route definition.
-
map.root :controller => "home"would be a shorter way of writing the path to your home directory. This will use / has the home, and not /home. If you still want to use /home (and home_path),map.home 'home', :controller => "home"will do the same thing.There’s a great guide written by Mike Gunderloy about everything there is to know about routing.