Routing helpers are made of one public method (#routes), available for actions, views and templates. It’s a factory to generate relative or absolute URLs, starting from named routes.

For a given route named :home, we can use home_path or home_url to generate relative or absolute URLs, respectively.

Usage

Imagine we have the following routes for our application:

  1. # apps/web/config/routes.rb
  2. root to: 'home#index'
  3. get '/foo', to: 'foo#index'
  4. resources :books

Relative URLs

We can do:

  1. <ul>
  2. <li><a href="<%= routes.root_path %>">Home</a></li>
  3. <li><a href="<%= routes.books_path %>">Books</a></li>
  4. </ul>

Which generates:

  1. <ul>
  2. <li><a href="/">Home</a></li>
  3. <li><a href="/books">Books</a></li>
  4. </ul>

We can’t link /foo, because it isn’t a named route (it lacks of the :as option).

Absolute URLs

  1. module Web
  2. module Controllers
  3. module Books
  4. class Create
  5. include Web::Action
  6. def call(params)
  7. # ...
  8. redirect_to routes.book_url(id: book.id)
  9. end
  10. end
  11. end
  12. end
  13. end

In the case above, we have passed a Hash as set of params that are required to generate the URL.

Absolute URL generation is dependent on scheme, host and port settings in apps/web/application.rb.