module Lucky::Redirectable
Overview
Redirect the request
There are multiple ways to redirect inside of an action. The most common ways are to redirect to a Lucky::Action
class, or a URL/path String
. Both use the #redirect
method:
redirect to: Users::Index
redirect to: Users::Show.with(user.id)
redirect to: "https://luckyframework.org/"
redirect to: "/users"
By default, the method will set the status code to 302
A.K.A. "Found". If you want to customize the status code, you can pass it directly:
redirect to: Users::Index, status: 301
# or use the built-in enum value
redirect to: Users::Index, status: HTTP::Status::MOVED_PERMANENTLY
Alternatively, the status code can also be configured globally through the redirect_status
setting:
Lucky::Redirectable.configure do |config|
config.redirect_status = 303
# or using a built-in enum value
config.redirect_status = HTTP::Status::SEE_OTHER.value
end
You can find a list of all possible statuses here.
Internally, all the different methods in this module eventually use the
method that takes a String
. However, it's recommended you pass a
Lucky::Action
class if possible because it guarantees runtime safety.
Included Modules
- Habitat::SettingsHelpers
- Habitat::TempConfig
Direct including types
Defined in:
lucky/redirectable.crConstant Summary
-
HABITAT_SETTINGS =
[{decl: redirect_status : Int32 = HTTP::Status::FOUND.value, example: nil, validation: nil}] of Nil
Class Method Summary
Instance Method Summary
-
#redirect(to route : Lucky::RouteHelper, status = Lucky::Redirectable.settings.redirect_status) : Lucky::TextResponse
Redirect using a
Lucky::RouteHelper
-
#redirect(to action : Lucky::Action.class, status = Lucky::Redirectable.settings.redirect_status) : Lucky::TextResponse
Redirect to a
Lucky::Action
-
#redirect(to path : String, status : HTTP::Status) : Lucky::TextResponse
Redirect to the given path, with a human friendly status
-
#redirect(to path : String, status : Int32 = Lucky::Redirectable.settings.redirect_status) : Lucky::TextResponse
Redirect to the given path, with an optional
Int32
status -
#redirect_back(*, fallback : Lucky::Action.class, status = Lucky::Redirectable.settings.redirect_status, allow_external = false) : Lucky::TextResponse
Redirect back with a
Lucky::Action
fallback -
#redirect_back(*, fallback : Lucky::RouteHelper, status = Lucky::Redirectable.settings.redirect_status, allow_external = false) : Lucky::TextResponse
Redirect back with a
Lucky::RouteHelper
fallback -
#redirect_back(*, fallback : String, status : HTTP::Status, allow_external = false) : Lucky::TextResponse
Redirect back with a human friendly status
-
#redirect_back(*, fallback : String, status : Int32 = Lucky::Redirectable.settings.redirect_status, allow_external : Bool = false) : Lucky::TextResponse
Redirects the browser to the page that issued the request (the referrer) if possible, otherwise redirects to the provided default fallback location.
- #settings
Class Method Detail
Instance Method Detail
Redirect using a Lucky::RouteHelper
redirect to: Users::Show.with(user.id), status: 301
Redirect to a Lucky::Action
redirect to: Users::Index
Redirect to the given path, with a human friendly status
redirect to: "/users", status: HTTP::Status::MOVED_PERMANENTLY
Redirect to the given path, with an optional Int32
status
redirect to: "/users"
redirect to: "/users/1", status: 301
Note: It's recommended to use the method above that accepts a human friendly version of the status
Redirect back with a Lucky::Action
fallback
redirect_back fallback: Users::Index
Redirect back with a Lucky::RouteHelper
fallback
redirect_back fallback: Users::Show.with(user.id)
Redirect back with a human friendly status
redirect_back fallback: "/users", status: HTTP::Status::MOVED_PERMANENTLY
Redirects the browser to the page that issued the request (the referrer) if possible, otherwise redirects to the provided default fallback location.
The referrer information is pulled from the 'Referer' header on the request. This is an optional header, and if the request is missing this header the fallback will be used.
redirect_back fallback: "/users"
A redirect status can be specified
redirect_back fallback: "/home", status: 301
External referers are ignored by default. It is determined by comparing the referer header to the request host. They can be explicitly allowed if necessary
redirect_back fallback: "/home", allow_external: true