Controller defined inside a module change all routes to its scope

177 views Asked by At

If I define a route inside a module, like this:

class DrivenSignupPlugin::AccountController < ApplicationController
  ...
end

Then all calls to url_for (including redirect_to) inside it will prefix the :controller parameter with driven_signup_plugin/.

This is not the desired behaviour as this controller uses many routes outside it. For example, render_access_denied is a method from ApplicationController.

1

There are 1 answers

0
brauliobo On BEST ANSWER

To workaround this, add this method to the controller:

  def default_url_options
    # avoid rails' use_relative_controller!
    {use_route: '/'}
  end 

For Rails 4.2+, something more complicated is needed:

  # inherit routes from core skipping use_relative_controller!
  def url_for options
    options[:controller] = "/#{options[:controller]}" if options.is_a? Hash and options[:controller]
    super options
  end 
  helper_method :url_for