Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mounted engine URLs are missing subdomain from constraint #34252

Closed
lukeredpath opened this issue Oct 18, 2018 · 1 comment
Closed

Mounted engine URLs are missing subdomain from constraint #34252

lukeredpath opened this issue Oct 18, 2018 · 1 comment

Comments

@lukeredpath
Copy link
Contributor

Steps to reproduce

Given an isolated engine called MyEngine, a namespaced controller called Foo::DashboardsController and the following routes:

constraints(subdomain: 'admin') do
  namespace: 'foo', path: 'portal' do
    root to: 'dashboards#index'
    mount MyEngine::Engine => '/my_engine'
  end
end

The MyEngine engine has a single WidgetsController and it's routes look like this:

root to: 'widgets#index'
resources :widgets

When trying to use URL helpers from outside a controller/view (e.g. in a service class or a job, on the console or anything outside of a request context), any generated URLs for my engine routes do not contain the subdomain while the routes for the main app do.

Expected behavior

From within a view, the following URL helpers output the following results:

foo_root_url => 'http://admin.example.com/portal'
main_app.foo_root_url => 'http://admin.example.com/portal'
foo_my_engine.root_url => 'http://admin.example.com/portal/my_engine'
foo_my_engine.widgets_url => 'http://admin.example.com/portal/my_engine/widgets'

All of these are what I would expect - the subdomain is included in the default URL options for anything within this constraint, the path set on the namespace is included and the routing helpers (including the engine helper object) require prefixing with the namespace to work.

Actual behavior

The problem occurs when trying to generate a URL for my engine, with the same output above, but say within a service class or a background job. Using the Rails console, I can generate the URLs for the main app as expected:

Rails.application.routes.url_helpers.foo_root_url => 'http://admin.example.com/portal'

However, foo_my_engine.root_url does not work:

Rails.application.routes.url_helpers.foo_my_engine.root_urll => NoMethodError: undefined method `foo_my_engine'

It seems I need to use #mounted_helpers to get access to the engine routes, however this is where the problem lies, with both the main_app and the engine helpers

include Rails.application.routes.mounted_helpers
main_app.foo_root_url => 'http://example.com/portal'
foo_my_engine.root_url => 'http://example.com/portal/my_engine'

I would expect these to output the same values that I get in my view, with the admin. subdomain included.

There may be an API I am missing but I've spent time digging through the Rails source to try and understand why I'm getting different results on the console (and in my service classes where I'm trying to actually generate the URLs) than in the view.

System configuration

Rails version: 5.2.1
Ruby version: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]

@rails-bot
Copy link

rails-bot bot commented Jan 27, 2019

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-2-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

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

No branches or pull requests

3 participants