Error on redirection from within router extension #2633

Closed
wildchild opened this Issue Aug 22, 2011 · 3 comments

Projects

None yet

2 participants

@wildchild
Contributor
match "/foo", to: lambda { |*args| redirect("/bar") }

results in:

can't convert String into Integer

Trace https://gist.github.com/7ce08e13388f271aa320

@pixeltrix
Member

Your code isn't quite right - try this instead:

match "/foo", to: redirect("/bar")

or if you need substitution:

match "/foo/:baz", to: redirect("/bar/%{baz}")

or if you need to use a block:

match "/foo/:baz", to: redirect do |params, request|
  #Do stuff and return string
  "/foo/#{params[:baz]}"
end
@pixeltrix pixeltrix closed this Aug 23, 2011
@wildchild
Contributor

I know about that. The problem is deeper. Is it possible to redirect from a lambda? Lambda in my snippet jast an example. Actualy there is some code:

module CouchDBAttachments
  def attachment(*args)
    lambda do |env|
      if Rails.env.production?
        [200, {"X-Accel-Redirect" => "/some/path"}, []]
      else
        redirect("http://localhost/road/to/nowhere")
      end
    end
  end
end

I know that it's possible to return:

[301, {"Location" => "http://localhost/road/to/nowhere"}, []]

but I suppose redirect should also work. Just like in Merb http://merbist.com/2009/07/13/ruby-authentication-of-couchdb-requests/

@pixeltrix
Member

Your lambda is basically a Rack app so it needs to return the proper Rack response. The redirect method is a routing helper that constructs and returns a proc that acts like a Rack app so you can't use it like you're using it above. You should be able call the proc manually, e.g:

module CouchDBAttachments
  def attachment(*args)
    lambda do |env|
      if Rails.env.production?
        [200, {"X-Accel-Redirect" => "/some/path"}, []]
      else
        redirect("http://localhost/road/to/nowhere").call(env)
      end
    end
  end
end

but it's just as easy to construct the response yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment