Michael Johnston
Michael Johnston

Reputation: 2382

Asset pipeline route helpers resolving to wrong URL or Path

I am having trouble with all my asset helpers except for image_tag for some reason. They are all generating the wrong paths. I am using Rails 4.0.0 and Ruby 2.0.0. My images are in /app/assets/images

e.g.

asset_url('this.png')    # -> /this.png
asset_path('this.png')   # -> /this.png
image_url('this.png')    # -> /images/this.png
image_path('this.png')   # -> /images/this.png
image-url('this.png')    # -> /images/this.png
asset-url('this.png')    # -> /this.png
image_tag('this.png')    # -> <img ... src="/assets/this.png" /> <- only correct one

I am always given the wrong URL... I need /assets/this.png which only seems to be generated by image_tag

This happens in .haml, .scss, .erb alike.

I can't find the solution to this problem... anybody seen this before and have the answer?

Upvotes: 5

Views: 1853

Answers (3)

Matthew Clark
Matthew Clark

Reputation: 1965

I also ran into this problem in development with a new Rail 4.2.1 application. I had:

body
{
    background: #EFEFEF image-url('layouts/background.jpg') no-repeat top center fixed;
}

and SASS was happily generating:

body
{
    background: #EFEFEF url('/images/layouts/background.jpg') no-repeat top center fixed;
}

After a couple hours of troubleshooting, I found this behavior was caused by a misspelling in the path! The directory containing the image I wanted was layout, not layouts.

Once I corrected the path, url(/assets/layout/background.jpg) was being generated as expected.

Update 12NOV2015 I ran into this again, this time with an image_tag(). I had the path spelled correctly (the image worked fine in development), but I had ".png" omitted form the file path. This works fine in development, but apparently not in production. Once I added the ".png" to the path, the image_tag() had the correct path.

This behavior is ridiculous. Not sure why it reverts to some non-standard path (/images instead of /assets) instead of throwing an exception (but I haven't bothered to look at the source, either).

Upvotes: 0

Gabriel Osorio
Gabriel Osorio

Reputation: 1063

I was having the same issue as you. I followed this thread and was able to solve it by clearing the tmp cache.

$ rake tmp:cache:clear

Hope it helps.

Upvotes: 4

Bijendra
Bijendra

Reputation: 10025

image_path has been deprecated.check the ApiDock

Method deprecated or moved
This method is deprecated or moved on the latest stable version. The last existing version (v3.2.13) is shown here.

These similar methods exist in v4.0.2:

ActionView::Helpers::AssetUrlHelper#image_path

For image_tag, the docs clearly says Returns an HTML image tag for the source. The source can be a full path or a file. So in that case path it points to is /assets/file_name.png

whereas for **image_path**Computes the path to an image asset in the public images directory. Full paths from the document root will be passed through.` Path is /images/filename.png

Upvotes: 1

Related Questions