Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

SCRIPT_NAME for main_app is ignored when URL helpers are called by isolated engine #6933

chewi opened this Issue Jul 2, 2012 · 5 comments


None yet
4 participants

chewi commented Jul 2, 2012

Say I have a Rails app mapped to /foo via my config.ru like so...

map "/foo" do
  run Foo::Application

This mostly works and I am able to log into rails_admin (mounted at /ra, accessed via /foo/ra) but I am unable to log out again because the URL for the log out button is just /admins/sign_out instead of /foo/admins/sign_out. This URL is the result of...


I didn't see anything immediately wrong with this and indeed, if I run it inside one of my own controllers, it returns the correct result. I did a lot of digging and found that the decision on whether to insert the prefix is made in actionpack/lib/action_controller/metal/url_for.rb.

      if _routes.equal?(env["action_dispatch.routes"])
        @_url_options.dup.tap do |options|
          options[:script_name] = request.script_name.dup

The check seems to establish whether we are generating a URL for this application/engine or another. It is necessary because otherwise it would return /foo/ra/admins/sign_out and that would also be wrong. We need some way of accessing the other application's SCRIPT_NAME so that can be prepended instead but I can't think of any way to do it.


chewi commented Jul 5, 2012

I don't fully understand how Rack works but if we can't access the instance of URLMap from this context then I guess URLMap needs to pass information about all the mappings down the stack through the environment?

@ghost ghost assigned drogus Jul 5, 2012

mvdamme commented Jul 9, 2012

I'm having the same problem: when running an app under a sub-uri (with the passenger RackBaseURI option), the main_app route helper in engine views generates urls that don't include the sub-uri (so SCRIPT_NAME is not included).

oneiros commented Aug 3, 2012

It might be worth noting that this bug works in both directions: If I want to use path helpers from my engine in my main application, they also ignore the SCRIPT_NAME.


drogus commented Aug 10, 2012

While going through code and examples in tests I noticed that actually our answer to this problem was to set default_script_options:

Rails.application.routes.default_url_options[:script_name] = '/foo'

I will update documentation to reflect this.

That said, I will try to implement better solution - the one above will work if app is mounted at only one url. Otherwise it will break. The problem is, the solution to this is not obvious and may get messy.

@drogus drogus closed this in de55da0 Aug 10, 2012

@drogus drogus reopened this Aug 10, 2012

@drogus drogus closed this in 5b3bb61 Aug 10, 2012


drogus commented Aug 10, 2012

I fixed this problem, but because I needed to change current behavior od default_url_options[:script_name] when used with SCRIPT_NAME in env I can't backport it to 3-2-stable. Until 4.0.0 release please use the solution that I wrote about in my previous comment. In 4.0.0 you can remove it and rails should do the right thing.

If you can run your apps on master, it would be really cool if you could test your cases.

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