简体   繁体   中英

Ruby on Rails - image_tag cannot find local image

I'm trying to display a logo image with image_tag , but it always comes up as a broken image.

// application.html.erb

<%= image_tag("logo.png") %>

I have "logo.png" in both /assets/images/logo.png and /assets/logo.png and have tried a lot of variations of the URL such as images/logo.png , /images/logo.png , assets/images/logo.png , etc but nothing seems to work.

Any ideas on what I could be doing wrong?

Thank you.

Edit: Addition information

I'm using rails ~> 5.0.1 with bootstrap-sass ~> 3.3.6 .

The file is located at /project_base/app/assets/images/logo.png .

Running rake assets:clobber & rake assets:precompie doesn't seem to change anything.

There is no generated asset for logo.png in /project_base/public/... .

I haven't changed anything in the development.rb file, but tried adding:

config.assets.digest = false 
config.assets.compile = true 

which still doesn't seem to effect the images.

try <%= image_tag 'logo.png' %>

Also, can I see more of your application.html.erb file?

I had this problem with Rails 5.0.2.

What eventually solved it is changing the version in the following line found, in my case, in config/assets.rb:

Rails.application.config.assets.version = '1.0'

According to http://guides.rubyonrails.org/asset_pipeline.html#upgrading-from-old-versions-of-rails it can be found in config/application.rb as well

config.assets.version = '1.0'

I guess the idea is that every time you update your assets you should update the asset version. I'm not sure it's worth it, so I'll probably will be removing the line completely.

You just need to put your image in assets/images/ and you can load it from any view by

<%=image_tag("logo.png")%>

if rails fails to find your image then it will throw an exception AssetNotFound

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM