Routing

Padrino provides advanced routing definition support to make routes and URL generation much easier. This routing system supports named route aliases and easy access to url paths. The benefits of this is that instead of having to hard-code route URLs into every area of your application, now we can just define the URLs in a single spot and then attach an alias which can be used to refer to the URL throughout the application.

Basic Routing Aliases

The routing system supports named aliases by using symbols instead of strings for your routes:

Demo::App.controllers do
  get :index do
    # url is generated as '/'
    # url_for(:index) => "/"
  end

  get :account, :with => :id do
     # url is generated as '/account/:id'
     # url_for(:account, id: 5) => "/account/5"
     # access params[:id]
  end
end

These routes can then be referenced anywhere in the application:

= link_to 'Index', url_for(:index)
= link_to 'Account', url_for(:account, id: 1)

Inline Route Alias Definitions

The routing plugin also supports inline route definitions in which the explicit URL and the named alias are both defined:

Demo::App.controllers do
  get :index, map: '/index/example' do
    # url_for(:index) => "/index/example"
  end

  get :account, map: '/the/accounts/:name/and/:id' do
    # url_for(:account, name: 'John', id: 5) => "/the/accounts/John/and/5"
    # access params[:name] and params[:id]
  end
end

Routes defined inline this way can be accessed and treated the same way as traditional named aliases:

= link_to 'Index Page', url_for(:index)
= link_to 'Account Page', url_for(:account, name: 'John', id: 1)

Namespaced Route Aliases

There is also support for namespaced routes which are organized into a named controller group:

Demo::App.controllers :admin do
  get :index do
    # url is generated as '/admin'
    # url_for(:admin, :index) => "/admin"
  end

  get :show, map: "/admin/:id" do
     # url is generated as "/admin/#{params[:id]}"
     # url_for(:admin, :show, id: 5) => "/admin/5"
  end
end

You can then reference these routes using the same url_for method:

= link_to 'admin index page', url_for(:admin, :index)
= link_to 'admin show page', url_for(:admin, :show, id: 25)

If you prefer explicit URLs to named aliases, that is also supported within a specified controller group:

Demo::App.controllers :admin do
  get '/show', name: :show do
    # url is generated as "/admin/show"
  end

  get '/other/:id', name: :other do
    # url is generated as "/admin/other/#{params[:id]}"
  end
end

You can then reference these routes using the same url_for method:

= link_to 'admin show page', url_for(:admin, :show)
= link_to 'admin index page', url_for(:admin, :other, id: 25)

Named Parameters

With Padrino you can also specify named parameters within your route definition:

Demo::App.controllers :admin do
  get :show, with: :id do
    # url is generated as "/admin/show/#{params[:id]}"
    # url_for(:admin, :show, id: 5) => "/admin/show/5"
  end

  get :other, with: [:id, :name]  do
    # url is generated as "/admin/other/#{params[:id]}/#{params[:name]}"
    # url_for(:admin, :other, id: 5, name: "hey") => "/admin/other/5/hey"
  end
end

You can then reference the URLs using the same url_for method:

= link_to 'admin show page', url_for(:admin, :show, id: 25)
= link_to 'admin other page', url_for(:admin, :other, id: 25, name: :foo)

Nested Routes

You can specify parent resources in padrino with the :parent option on the controller:

Demo::App.controllers :product, parent: :user do
  get :index do
    # url is generated as "/user/#{params[:user_id]}/product"
    # url_for(:product, :index, user_id: 5) => "/user/5/product"
  end

  get :show, with: :id do
    # url is generated as "/user/#{params[:user_id]}/product/show/#{params[:id]}"
    # url_for(:product, :show, user_id: 5, id: 10) => "/user/5/product/show/10"
  end
end

If need be the parent resource can also be specified on inline routes in addition:

Demo::App.controllers :product, parent: :user do
  get :index, parent: :project do
    # url is generated as "/user/#{params[:user_id]}/project/#{params[:project_id]}/product"
    # url(:product, :index, user_id: 5, project_id: 8) => "/user/5/project/8/product"
  end
end

Current Controller and Action

In a route, its filters and the views it renders, request.controller and request.action tell you which route is handling the request:

Demo::App.controllers :posts do
  before do
    @section = request.controller # => "posts"
  end

  get :show, with: :id do
    request.action # => :show
  end
end
  • request.controller is the controller's name as a string: "posts" here, and "admin/users" for controllers :admin, :users. It is nil for a route defined outside a controller.
  • request.action is the route's first argument as you wrote it: a symbol for a named route (:show), or the path string for a route such as get '/about' ("/about").
  • Both are nil when no route matched, for example in a not_found block reached that way. A route that halts with 404 or passes leaves them set to that route.
  • App-level before filters without conditions run once per request, so when a route passes to another one they keep the first route's values; controller-level filters and the route see the route that answers.

route returns the matched route itself: route.controller and route.action hold the same values, and route.name is the route's name (:"posts show"). route is nil when no route matched, so a layout that a not_found block renders should use request.controller, which is safe.

Since the controller is a string, compare it with a string, for example to mark the current section in a layout:

%li{ class: ('active' if request.controller == 'posts') }= link_to 'Posts', url(:posts, :index)

Type to search the guides.