module Lucky::Routable

Overview

Methods for routing HTTP requests and their parameters to actions.

Direct including types

Defined in:

lucky/routable.cr

Macro Summary

Macro Detail

macro delete(path) #

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:


[View source]
macro enforce_route_style(path, action) #

Implement this macro in your action to check the path for a particular style.

By default Lucky ships with a Lucky::EnforceUnderscoredRoute that is included in your BrowserAction and ApiAction (as of Lucky 0.28)

See the docs for Lucky::EnforceUnderscoredRoute to learn how to use it or disable it.


[View source]
macro fallback #

[View source]
macro get(path) #

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:


[View source]
macro inherit_route_settings #

[View source]
macro match(method, path) #

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.


[View source]
macro param(type_declaration) #

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?

  get "/posts" 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 < BrowserAction
  param token : String # this param is required!

  get "/user_confirmations/new" do
    # confirm the user with their `token`
  end
end

When visiting this page, the path must contain the token parameter: /user_confirmations/new?token=abc123


[View source]
macro patch(path) #

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:


[View source]
macro post(path) #

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:


[View source]
macro put(path) #

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:


[View source]
macro route_prefix(prefix) #

Sets the prefix for all routes defined by the match and http method (get, put, post, etc..) macros


[View source]
macro trace(path) #

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:


[View source]