开发者

How to get a named route from a Rack app hosted in Rails 3?

Is it possible to get the value of named route from with in a custom rack app when the app is mounted in rails 3 (in my case a Sinatra app)?

Simply using the route, (login_path) is throwing an exception for an undefined local variable.

UPDATE:

Here is an example, of what I am tryi开发者_如何学Cng to do:

before do
 redirect login_path unless some_condition
end

The app is mounted with

mount App.new, :at => '/path'

This part works as expected.

Thanks, Scott


Accessing the hosting rails app's routes in the mounted Sinatra might not be very elegant, since the hosted Sinatra should not have knowledge of the app that hosts it.

So instead, it'd better to do this in the rails app.

If you use devise, you can surround your mount block as this:

authenticate "user" do
  mount App.new, :at => '/path'
end

This can be done because devise itself is a middleware added before route.

Devise implements this as:

def authenticate(scope)
  constraint = lambda do |request|
    request.env["warden"].authenticate!(:scope => scope)
  end
  constraints(constraint) do
    yield
  end
end

If you don't use devise, you might need to implement something similar.


I think this is not possible because they have separate code. You're just telling to rails what to do with certain paths, an it routes those requests to another rack app, they don't share anything about internal code.

maybe you can write some code to tell Sinatra how to read rails routes. a good place to start from:

http://apidock.com/rails/ActionDispatch

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜