Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
tspaulino opened this Issue · 9 comments

5 participants

@tspaulino

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

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
Collaborator

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?

@tspaulino

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

@drogus
Collaborator

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

@rafaeldff

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
Collaborator

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

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

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
Collaborator

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

@drogus
Collaborator

Yup, closing.

@drogus drogus closed this
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.