instance method redirect

Ruby on Rails 3.0.20

Since v3.0.20 Last seen in v3.0.20

Signature

redirect(*args, &block)

Redirect any path to another path:

match "/stories" => redirect("/posts")

Parameters

args rest
block block
Source
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 359
        def redirect(*args, &block)
          options = args.last.is_a?(Hash) ? args.pop : {}

          path      = args.shift || block
          path_proc = path.is_a?(Proc) ? path : proc { |params| path % params }
          status    = options[:status] || 301

          lambda do |env|
            req = Request.new(env)

            params = [req.symbolized_path_parameters]
            params << req if path_proc.arity > 1

            uri = URI.parse(path_proc.call(*params))
            uri.scheme ||= req.scheme
            uri.host   ||= req.host
            uri.port   ||= req.port unless req.standard_port?

            body = %(<html><body>You are being <a href="#{ERB::Util.h(uri.to_s)}">redirected</a>.</body></html>)

            headers = {
              'Location' => uri.to_s,
              'Content-Type' => 'text/html',
              'Content-Length' => body.length.to_s
            }

            [ status, headers, [body] ]
          end
        end

Defined in actionpack/lib/action_dispatch/routing/mapper.rb line 359 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionDispatch::Routing::Mapper::HttpHelpers

Type at least 2 characters to search.

↑↓ navigate · open · esc close