Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Error on redirection from within router extension #2633

Closed
wildchild opened this Issue · 3 comments

2 participants

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

results in:

can't convert String into Integer

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

@pixeltrix
Owner

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
@wildchild

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
Owner

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
Something went wrong with that request. Please try again.