Triggering Another Route: calling call

Sometimes pass is not what you want, instead you would like to get the result of calling another route. Simply use call to achieve this:

get '/foo' do
  status, headers, body = call env.merge("PATH_INFO" => '/bar')
  [status, headers, body.map(&:upcase)]
end

get '/bar' do
  "bar"
end

Note that in the example above, you would ease testing and increase performance by simply moving "bar" into a helper used by both /foo and /bar.

If you want the request to be sent to the same application instance rather than a duplicate, use call! instead of call.

Check out the Rack specification if you want to learn more about call.

Casiano Rodriguez León 2015-01-07