You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The MyEngine engine has a single WidgetsController and it's routes look like this:
rootto: '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:
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:
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.
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.
Steps to reproduce
Given an isolated engine called
MyEngine
, a namespaced controller calledFoo::DashboardsController
and the following routes:The
MyEngine
engine has a singleWidgetsController
and it's routes look like this: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:
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:
However,
foo_my_engine.root_url
does not work:It seems I need to use
#mounted_helpers
to get access to the engine routes, however this is where the problem lies, with both themain_app
and the engine helpersI 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]
The text was updated successfully, but these errors were encountered: