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’



348
349
350
# File 'lib/action_dispatch/routing/mapper.rb', line 348

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’



318
319
320
# File 'lib/action_dispatch/routing/mapper.rb', line 318

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’



328
329
330
# File 'lib/action_dispatch/routing/mapper.rb', line 328

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’



338
339
340
# File 'lib/action_dispatch/routing/mapper.rb', line 338

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

#redirect(*args, &block) ⇒ Object

Redirect any path to another path:

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


355
356
357
358
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
# File 'lib/action_dispatch/routing/mapper.rb', line 355

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