Module: ActionDispatch::Routing::Mapper::HttpHelpers

Included in:
ActionDispatch::Routing::Mapper
Defined in:
lib/action_dispatch/routing/mapper.rb

Instance Method Summary collapse

Instance Method Details

#delete(*args, &block) ⇒ Object

Define a route that only recognizes HTTP PUT. For supported arguments, see match.

Example:

delete 'broccoli', :to => 'food#broccoli'


352
353
354
# File 'lib/action_dispatch/routing/mapper.rb', line 352

def delete(*args, &block)
  map_method(:delete, *args, &block)
end

#get(*args, &block) ⇒ Object

Define a route that only recognizes HTTP GET. For supported arguments, see match.

Example:

get 'bacon', :to => 'food#bacon'


322
323
324
# File 'lib/action_dispatch/routing/mapper.rb', line 322

def get(*args, &block)
  map_method(:get, *args, &block)
end

#post(*args, &block) ⇒ Object

Define a route that only recognizes HTTP POST. For supported arguments, see match.

Example:

post 'bacon', :to => 'food#bacon'


332
333
334
# File 'lib/action_dispatch/routing/mapper.rb', line 332

def post(*args, &block)
  map_method(:post, *args, &block)
end

#put(*args, &block) ⇒ Object

Define a route that only recognizes HTTP PUT. For supported arguments, see match.

Example:

put 'bacon', :to => 'food#bacon'


342
343
344
# File 'lib/action_dispatch/routing/mapper.rb', line 342

def put(*args, &block)
  map_method(:put, *args, &block)
end

#redirect(*args, &block) ⇒ Object

Redirect any path to another path:

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

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File '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