Padrino

Controllers

Prioritized Routes

Padrino (0.10.0+) has added support for respecting route order in controllers and also allows the developer to specify certain routes as less or more "important" then others in the route recognition order. Consider two controllers, the first with a "catch-all" route:

# app/controllers/pages.rb
Demo::App.controllers :pages do
  get :show, :map => '/*page' do
    "Catchall route"
  end
end

# app/controllers/projects.rb
Demo::App.controllers :projects do
MyApp.controller :projects do
  get :index do
    "Index"
  end
end

This wouldn't work by default because the second "/projects" endpoint would be eclipsed by the "/*page" catch-all route and as such projects would not be accessible. To solve this, you can do the following:

# app/controllers/pages.rb
Demo::App.controllers :pages do
  # NOTE that this route is now marked as low priority
  get :show, :map => '/*page', :priority => :low do
    "Catchall route"
  end
end

# app/controllers/projects.rb
Demo::App.controllers :projects do
  get :index do
    "Index"
  end
end

When setting a routes priority to :low, this route is then recognized lower then all "high" and "normal" priority routes. You are encouraged in cases where there is ambiguity, to mark key routes as :priority => :high or catch-all routes as :priority => :low in order to guarantee expected behavior.

last updated: 2016-11-26

comments powered by Disqus