Rails.application.routes.url_for doesn't find routes for engine #6389

tspaulino opened this Issue May 18, 2012 · 10 comments


None yet
6 participants

In my application I have an engine with routes defined inside it like the code below:

ProstPoll::Engine.routes.draw do
  resources :polls do
    resources :answers, :only => :update

Inside the engine we're using isolate_namespace ProstPoll to allow routes to be used without namespaces. When trying access it, rails application tells that couldn't find our routes and return it with assets routes and the parameters to generate url (controller, action, id, etc) instead of the declared ones on the engine (something like "http://ourdomain.com/assets?controller=foo&action=bar&id=anyone"). Does it is a bug for routes or we're doing something wrong when creating this routes and namespaces?


drogus commented May 18, 2012

If you use Rails.application.routes.url_for as you described in the topic, it won't work, becuase this is the different route set. You should use ProstPoll::Engine.routes.url_for.

btw, why do you want to use url_for directly on routes?

I use it to generate a url and put this into a queue. I use it into a module.


drogus commented May 18, 2012

@tspaulino could you try using ProstPoll::Engine.routes.url_for? Let me know if that worked.

I'm on @tspaulino's team, let me try to clarify the problem. The code that calls url_for is shared among all engines; it is on a module that's mixed in our model classes. The shared method can call Rails.application.routes.url_for as it is not specific to the engine.


drogus commented May 19, 2012

@rafaeldff I'm afraid there is no easy way to make this work. As I mentioned, route sets are different for engines and application. If it was done with one route set, we would end up with conflict issues (ie. some paths can overwrite other paths, this is almost always true for root_path. If you need to call routes from models, your best bet is to make the method that uses url_for aware of the namespace and get the route from proper routes.

Another thing is that using routes in models is considered a code smell, are you sure that's the best way to achieve your goals?


pixeltrix commented May 19, 2012

@tspaulino @rafaeldff I think you're not understanding what @drogus is saying - the routes aren't in Rails.appliation.routes they're in ProstPoll::Engine.routes. If you wanted to access the engine's routes in a view for example you'd need to do prost_proll.url_for not just url_for. You can draw the routes for the engine on the application's routeset if you want to - just change ProstPoll::Engine to Rails.application in the engine's routes.rb file.


drogus commented May 19, 2012

just change ProstPoll::Engine to Rails.application in the engine's routes.rb file.

But remember that in this case you will end up with shared routes, which is probably not something that you want in isolated application. The other thing is that what @pixeltrix suggests may be a good direction - if you want to use routes from all of the engines, maybe they should not be isolated?


schneems commented Sep 7, 2012

I think @drogus and @pixeltrix explained this scenario well, can we close this issue?


drogus commented Sep 7, 2012

Yup, closing.

@drogus drogus closed this Sep 7, 2012

@drogus this is not working for me. Check out my question on stackoverflow: http://stackoverflow.com/questions/38535727/how-to-get-url-for-working-with-a-module

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