Module: Hanami::Action::Redirect

Defined in:
gems/gems/hanami-controller-1.3.2/lib/hanami/action/redirect.rb,
gems/gems/hanami-controller-1.3.3/lib/hanami/action/redirect.rb

Overview

HTTP redirect API

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#redirect_to(url, status: 302) ⇒ Object (private)

Redirect to the given URL and halt the request

Examples:

With default status code (302)

require 'hanami/controller'

class Create
  include Hanami::Action

  def call(params)
    # ...
    redirect_to 'http://example.com/articles/23'
  end
end

action = Create.new
action.call({}) # => [302, {'Location' => '/articles/23'}, '']

With custom status code

require 'hanami/controller'

class Create
  include Hanami::Action

  def call(params)
    # ...
    redirect_to 'http://example.com/articles/23', status: 301
  end
end

action = Create.new
action.call({}) # => [301, {'Location' => '/articles/23'}, '']

Parameters:

  • url (String)

    the destination URL

  • status (Fixnum)

    the http code

See Also:

Since:

  • 0.1.0

def redirect_to(url, status: 302)
  headers[LOCATION] = ::String.new(url)
  halt(status)
end