module Orion::DSL::Helpers
Overview
Route helpers provide type-safe methods to generate paths and URLs to defined routes
in your application. By including the Helpers
module on the router (i.e. MyApplicationRouter::Helpers
)
you can access any helper defined in the router by {{name}}_path
to get its corresponding
route. In addition, when you have a @context : HTTP::Server::Context
instance var,
you will also be able to access a {{name}}_url
to get the full URL.
scope "users", helper_prefix: "user" do
get "/new", to: "users#new", helper: "new"
end
class UsersController < BaseController
def new
File.open("new.html") { |f| IO.copy(f, response) }
end
def show
user = User.find(request.path_params["id"])
response.headers["Location"] = new_user_path
response.status_code = 301
response.close
end
end
Making route helpers from your routes
In order to make a helper from your route, you can use the helper
named argument in your route.
scope "users" do
get "/new", to: "Users#new", helper: "new"
end
Using route helpers in your code
As you add helpers they are added to the nested Helpers
module of your router.
you may include this module anywhere in your code to get access to the methods,
or call them on the module directly.
If @context : HTTP::Server::Context
is present in the class, you will also be
able to use the {helper}_url
versions of the helpers.
resources :users
class User
include RouteHelpers
def route
user_path user_id: self.id
end
end
puts RouteHelpers.users_path