Ruby on Rails/Routing

Because Routing is such an important part of Rails we dedicated a whole chapter to Routing (even though they are part of ActionView).

For example, if you want to display the product with the id 4, you will use a link similar to products/4. So Rails will figure out, that you want to show the information that belongs to the product with the id 4.

Routing also works when you want to link somewhere from one point of your application to another point. If you want to go back from the products view to the index overview that displays all products, you may place something like this in your view: <%= link_to 'Back', products_path %>

When writing your own routes inside routes.rb, keep in mind, the lower the route is inside your file, the less priority it has.

RESTful routes
RESTful routes are the default routes in Rails. To get a more detailed technical view on REST, check out the Wikipedia article.

Basically REST provides a way of communication inside your application and all requests that exist from external sources (just as a browser request). To understand these principles better, take a look the following table:

As you can see, all the actions of REST are already in our scaffolded controller. Keep in mind that RESTful routes reference a single object (products in this case). These 7 actions would result in a single route inside routes.rb:

With a RESTful resource, it's easy to link the different views together or link to a specific view. With REST, Rails provides us some helpers to get to our desired location:


 * products_url & products_path => redirects us to the index overview and edit view for our products (note the plural)
 * new_product_url & new_product_path => will lead as to the form that creates a new product
 * edit_product_url & edit_photo_path => provides us with an edit-form for a specific product
 * product_url & product_path => is responsible for showing, deleting and updating a product

While *_path will create a relative path to, *_url provides the whole URL
 * _path => /products
 * _url => http://localhost/products

You will very likely need more than one REST route. You can easily write multiple REST routes into a single:

You will come across many similar constructs like our products-category relation:

As with resources that are not nested, you will be able to access all *_url and *_path helpers e.g. products_categories_path or new_product_category_url

These path need to be present in your routes.rb in an similar maner to your model:

if you need to add more than one association, put these in []

which is the same as the following route, only shorter

With this way, you can nest as many resources as you want, but you should try to keep the level of nesting as low as possible. So one nested resource is ok, but try to avoid 2 or more. To avoid these problems, we can use "shallow nesting"

If we want to add supplier for specific categories, we may end up with something like

or in short:

There are many more advanced features for routing. More infos and instructions can be found in the official Rails guides.

Regular Routes
Even though RESTful routes are the encouraged way to go, you can also use regular routes inside your application. When working with regular routes, you give Rails some keywords and it will map the proper path for you. One of these default routes is already inside your routes.rb (this time we don't use .resources but .connect)

will for example be a browser request similar to <tt>products/show/2</tt>

If you are familiar with other languages that focus an the web, you may wonder how query strings inside the URL are handles: Rails automatically provides these parameters inside the params hash

So if you take the example above and add <tt>products/show/2?category=3</tt>, we would be able to access the category id with <tt>params[:category_id]</tt>.