asset_path helper not including relative url when used in .css.erb #3365

Closed
pokonski opened this Issue Oct 19, 2011 · 10 comments

Comments

Projects
None yet
6 participants
Contributor

pokonski commented Oct 19, 2011

Hi!

I'm using Rails 3.1.1 and Passenger to deploy an application to /community sub-uri. In regular views this:

<%= asset_path "image.png" %> 

transforms into proper path:

/community/assets/image.png

But the same helper in my style.css.erb file produces:

/assets/image.png

I've seen a lot of older issues similar to this one, but none of them fixed this, so far. This basically renders asset_path useless in stylesheets, for now I'm resorting to asset_data_uri for inline inclusion.

EDIT: Same thing applies when using <%= asset_path "image.png" %> inside .js.erb files.

pdfrod commented Nov 6, 2011

I'm having the same problem. Using:

<%= asset_path('menu/down.gif') %>

renders the path:

/assets/menu/down-9d4f620917ddfbf1f1b2213f83a432b9.gif

instead of the expected:

/suburl/assets/menu/down-9d4f620917ddfbf1f1b2213f83a432b9.gif

ginjo commented Nov 10, 2011

I've been having the same or similar problem since 3.1 was released. Resolution of relative url roots for assets using css/sass/scss/erb appears to be on-again-off-again. There are several similar threads about this issue. Some of them:

http://stackoverflow.com/questions/7363733/rails-3-1-sass-asset-helpers-not-not-including-rails-relative-url-root-relativ
http://stackoverflow.com/questions/7293918/broken-precompiled-assets-in-rails-3-1-when-deploying-to-a-sub-uri
https://github.com/rails/rails/pull/3294
https://github.com/rails/rails/issues/3259

With Rails 3.1.0, I solved the problem with the Sass::Rails::Helpers#public_path fix, mentioned in the above links. But with Rails 3.1.1, this fix no longer appears effective.

For me, with a fresh install of Rails 3.1.1 and all of it's dependencies, "image-url" in a scss file ignores the ENV['RAILS_RELATIVE_URL_ROOT'] environment variable, even though all of my view helpers resolve it correctly. Interestingly, "asset-url" DOES work.

In application.css.scss

image-url('myImage.png') 

resolves to

url('/assets/myImage.png')

while

asset-url('myImage.png', 'image')

resolves to

url('/subURI/assets/myImage.png')

I'm having the same problem with

<%= asset_path('zurb-nub-shell-hover.png') %>
image-url('zurb-nub-shell-hover.png')

But as ginjo said, asset-url works correctly so long as you are running the monkey patch here:
http://stackoverflow.com/questions/7293918/broken-precompiled-assets-in-rails-3-1-when-deploying-to-a-sub-uri.

I'm running Rails 3.1.3.

Contributor

pokonski commented Nov 30, 2011

But image-url requires SASS and I don't use that. Issue still stands, seems like a strictly Passenger+sprockets+erb issue.

I agree, it's still an issue. We don't use SASS either, but it's the only workaround we currently have short of not using the asset pipeline.

Contributor

pokonski commented Nov 30, 2011

I guess it's time to dive into the code and fix it...

awendt commented Dec 1, 2011

+1

Resolution of relative url roots for assets using css/sass/scss/erb appears to be on-again-off-again

That's my impression, too. Would be great if this could be fixed for good. Plus, Stackoverflow is full of work-arounds which all appear to be outdated despite their young age.

Contributor

pokonski commented Dec 11, 2011

Gonna go ahead and harras José for a second. @josevalim, would you merge a fix for it if I made one? Don't want to code in vain so just asking!

Contributor

pokonski commented Dec 12, 2011

Ok guys, I have some good news. I managed to fix the asset_path(and every other that generates paths as all use it) method in .erb assets in development mode. Basically what was happening is that Passenger uses ENV['RAILS_RELATIVE_URL_ROOT'] which was ignored by Sprockets.

With my fix (https://github.com/pokonski/rails/commit/53cebf7eb80d6b97c21c421cbc7d1f572170e33f) it now generates proper paths for assets in sub-uri.

For production you still need to set that ENV variable before precompiling assets, just do this:

RAILS_RELATIVE_URL_ROOT="/suburi" rake assets:precompile

josevalim closed this in 5266eb9 Dec 12, 2011

josevalim closed this in af308ff Dec 12, 2011

Nice!

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