I've been learning Rails but routes continues to confuse the heck out of me.
The thing that makes it most confusing, I think, is that the routes you define are sensitive to where they are defined in your routes.rb
file relative to other routes.
Has anyone come across a nice simple guide that sums things up well?
TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.
Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. A single call to resources can declare all of the necessary routes for your index , show , new , edit , create , update , and destroy actions.
The :as option creates a named path. You can then call this path in your controllers and views (e.g. redirect_to things_path ).
In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as. map.resources :photos. creates seven different routes in your application: HTTP verb.
The first hit on Google for "Rails routes guide" is Rails Routing from the Outside In, which is quite comprehensive.
If you're okay with spending money on a dead tree reference, The Rails Way is actually worth it. The guides posted are probably your best bet this time, but if you plan on doing a lot of Rails, this book really breaks it down and makes it understandable. It's been a tremendous help for me. Good Luck.
As a side remark:
The routes at the beginning take preference over the routes later in the file. So whenever you want to specify some kind of catchall route (like the default routes that map every action in every controller) you need to do that at the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With