Reputation: 3000
In this Stack Overflow question, the end of the question begins to address this issue, but it wasn't the main question, which was answered there already.
I have a module that's included both in ApplicationController
and in MyEngineController
. Suppose some of the module's instance methods use login_url
or some other named route. It works from ApplicationController but not from MyEngineController
. I could replace that with main_app.login_url
, which will make it work for both, but that seems very verbose, and anyway, what if the code is already part of a gem dependency? How do I make it work post-facto from the engine I'm coding?
When I try using any named helper, here's what I get:
ActionController::RoutingError:
No route matches {:action=>"new", :controller=>"sessions"}
actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:532:in `raise_routing_error'
actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:528:in `rescue in generate'
actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:520:in `generate'
actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:561:in `generate'
actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:586:in `url_for'
actionpack-3.2.8/lib/action_dispatch/routing/url_for.rb:148:in `url_for'
actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:213:in `login_url'
Note that :controller=>"sessions"
would have to be :controller=>"/sessions"
(with the slash to put it at root namespace), but that's not an option I can pass to named routes AFAIK. I tried adding this code:
extend ActiveSupport::Concern
included do
main_app.install_helpers(self)
end
in MyEngineController, but it didn't help. (install_helpers
is very poorly documented. Can anyone explain what it's supposed to do?)
Upvotes: 2
Views: 769
Reputation: 3000
For now, I'm going with this solution (in MyEngineController):
protected
def url_for options=nil
begin
super options
rescue ActionController::RoutingError
main_app.url_for options
end
end
I'm not such a fan of it because, in general, exceptions shouldn't be used for flow control. So what other suggestions do you have?
Upvotes: 2