module Lucky::Routable
Overview
Methods for routing HTTP requests and their parameters to actions.
Direct including types
Defined in:
lucky/routable.crMacro Summary
-
delete(path)
Define a route that responds to a DELETE request
- fallback
-
get(path)
Define a route that responds to a GET request
- inherit_route_settings
-
match(method, path)
Define a route with a custom HTTP method.
-
nested_route
Define a nested route that responds to the appropriate HTTP request automatically
-
param(type_declaration)
Access query and POST parameters
-
patch(path)
Define a route that responds to a PATCH request
-
post(path)
Define a route that responds to a POST request
-
put(path)
Define a route that responds to a PUT request
-
route
Define a route that responds to the appropriate HTTP request automatically
-
route_prefix(prefix)
Sets the prefix for all routes defined by the match and http method (get, put, post, etc..) macros
-
trace(path)
Define a route that responds to a TRACE request
Macro Detail
Define a route that responds to a DELETE request
Use these methods if you need a custom path or are using a non-restful route. For example:
class Profile::ImageUpload
delete "/profile/image/:id" do
# action code here
end
end
will respond to an HTTP DELETE
request.
See also our guides for more information and examples:
Define a route that responds to a GET request
Use these methods if you need a custom path or are using a non-restful route. For example:
class Profile::ImageUpload
get "/profile/image/:id" do
# action code here
end
end
will respond to an HTTP GET
request.
See also our guides for more information and examples:
Define a route with a custom HTTP method.
Use this method if you need to match a route with a custom HTTP method (verb). For example:
class Profile::Show
match :options, "/profile" do
# action code here
end
end
Will respond to an `HTTP OPTIONS` request.
Define a nested route that responds to the appropriate HTTP request automatically
This works similarly to route
but it will provide multiple parameters.
For example:
class Posts::Comments::Show
nested_route do
plain_text "Post: #{post_id}, Comment: #{comment_id}"
end
end
This action responds to the /posts/:post_id/comments/:comment_id
path.
Access query and POST parameters
When a query parameter or POST data is passed to an action, it is stored in
the params object. But accessing the param directly from the params object
isn't type safe. Enter param
. It checks the given param's type and makes
it easily available inside the action.
class Posts::Index < BrowserAction
param page : Int32?
route do
plain_text "Posts - Page #{page || 1}"
end
end
To generate a link with a param, use the with
method:
Posts::Index.with(10).path
which will generate /posts?page=10
. Visiting
that path would render the above action like this:
Posts - Page 10
This works behind the scenes by creating a page
method in the action to
access the parameter.
Note: Params can also have a default, but then their routes will not
include the parameter in the query string. Using the with(10)
method for a
param like this:
param page : Int32 = 1
will only generate /posts
.
These parameters are also typed. The path /posts?page=ten
will raise a
Lucky::InvalidParamError
error because ten
is a String not an
Int32.
Additionally, if the param is non-optional it will raise the
Lucky::MissingParamError
error if the required param is absent
when making a request:
class UserConfirmations::New
param token : String # this param is required!
route do
# confirm the user with their `token`
end
end
When visiting this page, the path must contain the token parameter:
/user_confirmations?token=abc123
Define a route that responds to a PATCH request
Use these methods if you need a custom path or are using a non-restful route. For example:
class Profile::ImageUpload
patch "/profile/image/:id" do
# action code here
end
end
will respond to an HTTP PATCH
request.
See also our guides for more information and examples:
Define a route that responds to a POST request
Use these methods if you need a custom path or are using a non-restful route. For example:
class Profile::ImageUpload
post "/profile/image/:id" do
# action code here
end
end
will respond to an HTTP POST
request.
See also our guides for more information and examples:
Define a route that responds to a PUT request
Use these methods if you need a custom path or are using a non-restful route. For example:
class Profile::ImageUpload
put "/profile/image/:id" do
# action code here
end
end
will respond to an HTTP PUT
request.
See also our guides for more information and examples:
Define a route that responds to the appropriate HTTP request automatically
class Posts::Show
route do
plain_text "Post: #{post_id}"
end
end
This action responds to the /posts/:post_id
path.
Each route needs a few pieces of information to be created:
- The HTTP method, like
GET
,POST
,DELETE
, etc. - The path, such as
/users/:user_id
- The class to route to, like
Users::Show
The route
method will try to determine these pieces of information based
the class name. After it knows the class, Lucky will transform the full
class name to figure out the path, i.e. removing the ::
separators and
adding underscores. The method is found via the last part of the class name:
Index
->GET
Show
->GET
New
->GET
Create
->POST
Edit
->GET
Update
->PUT
Delete
->DELETE
If you are using a non-restful action name you should use the get
, put
,
post
, or delete
methods. Otherwise you will see an error like this:
Could not infer route for User::ImageUploads
See also our guides for more information and examples:
Sets the prefix for all routes defined by the match and http method (get, put, post, etc..) macros
Define a route that responds to a TRACE request
Use these methods if you need a custom path or are using a non-restful route. For example:
class Profile::ImageUpload
trace "/profile/image/:id" do
# action code here
end
end
will respond to an HTTP TRACE
request.
See also our guides for more information and examples: