Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 256
Scott Watermasysk Avatar asked Apr 13 '11 01:04

Scott Watermasysk


People also ask

How do I find a specific route in Rails?

Controller specific searchUse option -c to search for routes related to controller. Also remember that Rails does case insensitive search. So rails routes -c users is same as rails routes -c Users.

What does rake routes do?

rake routes will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you're trying to get familiar with.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.


1 Answers

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.

like image 142
James Chen Avatar answered Oct 12 '22 21:10

James Chen