简体   繁体   中英

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 trying 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM