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

ActiveStorage is not compatible with relative_root_urls #43633

Open
taa-lze opened this issue Nov 12, 2021 · 2 comments
Open

ActiveStorage is not compatible with relative_root_urls #43633

taa-lze opened this issue Nov 12, 2021 · 2 comments

Comments

@taa-lze
Copy link

taa-lze commented Nov 12, 2021

Bug

If you have configured a relative root url, the active storage service will redirect wrong.
Example:

http://localhost:3000/anything/rails/active_storage/blobs/blob_hash/image.jpg
Will get redirected to:
http://localhost:3000/rails/active_storage/disk/blob_hash/image.jpg
Instead of:
http://localhost:3000/anything/rails/active_storage/disk/blob_hash/image.jpg

Steps to reproduce

Add to config:
config.relative_url_root = '/anything'
In your view:
image_tag(record.image)

System configuration

Rails version: 6.1.0

Ruby version: ruby 2.7.1p83

@taa-lze taa-lze changed the title ActoveStorage is not compatible with relative_root_urls ActiveStorage is not compatible with relative_root_urls Nov 12, 2021
@taa-lze
Copy link
Author

taa-lze commented Nov 12, 2021

Workaround:

def direct_file_url(attachment)
    ActiveStorage::Current.host = request.base_url
    ActiveStorage::Blob.service.url(attachment.key, disposition: 'inline', content_type: attachment.content_type, filename: attachment.filename, expires_in: 20_000)
                       .sub!(request.base_url, "#{request.base_url}#{Rails.application.config.relative_url_root}")
end

@abeidahmed
Copy link
Contributor

abeidahmed commented Dec 11, 2021

This is still prevalent in rails 7.1.0.alpha and is not just related to active storage. It also redirects to the wrong URL for JS and CSS if I override the config.

Started GET "/anything/assets/application-5b98c5413b25e84aff009af9d2ca59257596e92d67359343c32a2a3514453b29.css" for 127.0.0.1 at 2021-12-11 12:02:54 +0530
Started GET "/anything/assets/es-module-shims.min-5bee6b190e63d2c7cb18661d714ede9c7ab8ce0b4b3e5cc34092e05ef6046ebd.js" for 127.0.0.1 at 2021-12-11 12:02:54 +0530

ActionController::RoutingError (No route matches [GET] "/anything/assets/es-module-shims.min-5bee6b190e63d2c7cb18661d714ede9c7ab8ce0b4b3e5cc34092e05ef6046ebd.js"):


ActionController::RoutingError (No route matches [GET] "/anything/assets/application-5b98c5413b25e84aff009af9d2ca59257596e92d67359343c32a2a3514453b29.css"):

Started GET "/anything/assets/image-3b62fb9bcec45d4a137737f821fb5db5c6f42b7d1dd01ffb3ba6a066bda87cc8.jpg" for 127.0.0.1 at 2021-12-11 12:02:54 +0530

ActionController::RoutingError (No route matches [GET] "/anything/assets/image-3b62fb9bcec45d4a137737f821fb5db5c6f42b7d1dd01ffb3ba6a066bda87cc8.jpg"):

As defined here, it uses the ENV["RAILS_RELATIVE_URL_ROOT"] to set the relative_url_root. So I think you need to do the same instead of overriding it in the config. You can also check this guide.

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