Method: Hanami::API::Block::Context#redirect

Defined in:
lib/hanami/api/block/context.rb

#redirect(url, status = 301) ⇒ Object

Redirects request and immediately halts it

Examples:

URL

get "/legacy" do
  redirect "/dashboard"

  # this code will never be reached
end

# It sets a Rack response: [301, {"Location" => "/new"}, ["Moved Permanently"]]

URL and HTTP status

get "/legacy" do
  redirect "/dashboard", 302

  # this code will never be reached
end

# It sets a Rack response: [302, {"Location" => "/new"}, ["Moved"]]

Parameters:

  • url (String)

    the destination URL

  • status (Integer) (defaults to: 301)

    an optional HTTP code for the redirect

See Also:

Since:

  • 0.1.0



86
87
88
89
# File 'lib/hanami/api/block/context.rb', line 86

def redirect(url, status = 301)
  headers["Location"] = url
  halt(status)
end