rails: Wrong documented - path to images when using image_tag, image_path helper

It is indicated in the doc that when using image_path helper the generated HTML will be as follows:

image_path("icons/edit.png")                               # => "/assets/icons/edit.png"

The image_path is used internally by image_tag helper. But it is not really the case, - the generated HTML code contains imagesprefix and will be as follows:

= image_tag('icons/multiple-devices.svg') => #/images/icons/multiple-devices.svg

I put multiple-devices.vg file into assets/icons. What’s wrong with that?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

Here’s the deal. You’ve got that asset under app/assets/app_icons which isn’t in Sprocket’s search path.

Then you’re looking for

<%= image_path('app_icons/evolution.svg')%>

Sprockets would expect this file in

app/assets/app_icons/app_icons

Since it starts looking at the root of each of your paths.

The solution is to add that folder to your paths:

    # config/application.rb
    config.assets.paths << Rails.root.join("app/assets/app_icons")
    config.assets.precompile << "app_icons/evolution.svg"

Then in your view use

<%= image_path('evolution.svg')%>

Takewaway

You’re using the API incorrectly, however this seems like something we could warn you about or raise an error. Maybe in development we check to see if we could find an asset by that name “evolution.svg” and tell you that while we didn’t find it where we were looking we did find it in <path>. Though that would have made things more confusing in this case. I’ll need to think a bit more on it. I’m going to close this issue and open a new one in sprockets.