Add example of using url_helpers in resolvers #4009
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A problem I’ve been struggling with is generating URLs within the resolvers using the current host and port. A common solution is to set
Rails.application.routes.default_url_options
, but this is static and with my current setup I need to be able to return the host actually used within the request (My development environment runs over both http and https, so I can’t just hardcode localhost:3000).A solution I’ve found is to pass the controller’s request into the context, and use the
ActionController::UrlFor
concern.And likewise for Active Storage,
ActiveStorage::SetCurrent
works similarly.I thought it might be useful to share this with anyone else running into this use case.