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] url_for returns path, not URL #32948

Closed
jacobsmith opened this issue May 21, 2018 · 7 comments
Closed

[ActiveStorage] url_for returns path, not URL #32948

jacobsmith opened this issue May 21, 2018 · 7 comments

Comments

@jacobsmith
Copy link
Contributor

  • I'm not sure if this is a bug, me not understanding the API, or something in between. If it is found to be a bug, I'd be happy to work on a PR if some direction can be given as to how to handle what would be a breaking API change.

Steps to reproduce

Call url_for on an active storage object ( url_for(user.avatar) )

Expected behavior

The returned string will be a fully qualified URL. ( eg. https://myserver.com/rails_storage/blob/foobar )

Actual behavior

Only the path is returned:

/rails_storage/blob/foobar

System configuration

Rails version: 5.2.0

Ruby version: 2.3.3p222

@jacobsmith jacobsmith changed the title url_for returns path, not URL [ActiveStorage] url_for returns path, not URL May 21, 2018
@georgeclaghorn
Copy link
Contributor

georgeclaghorn commented May 21, 2018

This is a documented quirk of url_for:

Note that by default :only_path is true so you'll get the relative “/controller/action” instead of the fully qualified URL like “example.com/controller/action”.

To generate a fully-qualified URL, use polymorphic_url(user.avatar).

@jacobsmith
Copy link
Contributor Author

Ah, thank you @georgeclaghorn ! I missed that in the documentation somehow. 👍

@dvanoni
Copy link

dvanoni commented Jan 15, 2019

Would it be worthwhile to add a note about this to the Active Storage Overview guide page?

I believe a note about this could be beneficial for those (like myself) using Rails as an API. When returning a JSON response with a URL to a file, a fully-qualified URL probably makes the most sense. It took me a while to hunt down this issue and realize I needed to use polymorphic_url instead of url_for as the guide describes.

@michelson
Copy link
Contributor

michelson commented Mar 6, 2019

It seems rails 5.2.2 has the oposite default for only_path on url_for , the default is false, so now there is no way to generate relative path for an attachment on url_for unless we set that into the Rails.application.routes.default_url_options

@pastullo
Copy link

Very confusing but indeed polymorphic_url will work!

@brunoprietog
Copy link
Contributor

I agree that it would be good to document this in the overview active storage guide.

@unRARed
Copy link

unRARed commented Aug 1, 2022

thank you! polymorphic_url also worked for me. i agree with brunoprietog this quirk should be added from https://edgeguides.rubyonrails.org/active_storage_overview.html. it seems strange to me that url_for would ever return a path, seems like that would be path_for.

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

7 participants