Method: Gin::Controller#rewrite

Defined in:
lib/gin/controller.rb

#rewrite(*args) ⇒ Object

Halt execution of the current controller action and create a new request to another action and/or controller, or path.

Raises Gin::RouterError if the controller and action don’t have a route. Returns a 404 response if an unrecognized path is given.

The rewrite method acts just as if a request had been sent from the client, and will re-run any of the in-app middleware. If the app is itself running as middleware, you may use rewrite to pass a request down to the next item in the stack by specifying a path not supported by the app.

Supports the same arguments at the Gin::Controller#url_to method.

rewrite MyController, :action
#=> Calls app with route for MyController#action

rewrite MyController, :show, :id => 123
#=> Calls app with route for MyController#action with the given params

rewrite :show_foo
#=> Calls app with route for the current controller's :show_foo action,
#=> or if missing the controller and action for the :show_foo named route.

# Rewrite and execute the request with the given headers.
rewrite :show_foo, 'HTTP_X_CUSTOM_HEADER' => 'foo'
rewrite :show_foo, params, 'HTTP_X_CUSTOM_HEADER' => 'foo'

# Rewrite to an arbitrary path.
rewrite '/path/to/something/else', {}, 'REQUEST_METHOD' => 'POST'

Note that params are not forwarded with the rewrite call. The app considers this to be a completely different request, which means all params required must be passed explicitely.

Streamed and IO request content is also ignored unless it is explicitely assigned to the ‘rack.input’ (as a part of the headers argument).



601
602
603
604
605
# File 'lib/gin/controller.rb', line 601

def rewrite *args
  args.unshift(self.class) if Symbol === args[0] &&
                              self.class.actions.include?(args[0])
  halt(*@app.rewrite!(@env, *args))
end