简体   繁体   中英

Ruby on rails 4 assets images cannot be loaded

Currently, I am using ruby on rails 4.

In the html part, I am trying to load an image:

<img width="121" height="31" src="/assets/images/flatty/logo_lg.svg" />  

The image file: logo_lg.svg is in the location: app/assets/images/flatty/

I tried:

<%= image_tag "/assets/images/flatty/logo_lg.png" %>

And in production.rb file: config.serve_static_assets = true .

Moreover, tried: rails assets:precompile

But the system still cannot find the image file.

You have a .svg but you are trying to load a .png . Change your code to this:

<%= image_tag "flatty/logo_lg.svg" %>

Don't use your full directory when you load images. The asset pipeline does that for you. Just put them in the right place ie the images directory and write down:

<%= image_tag "someimg.png" %>

Additionally, if you want to style your image, you can give it a class or an id ( id would fit more for a logo):

<%= image_tag "flatty/logo_lg.svg" , :id => 'someId' , :class => 'someClass' %>

And then just put the CSS in your application css file or the one generated by the controller of you page:

  .someClass {

  width: 50px;
  height: 50px;

 }


 #someId {

  width: 50px;
  height: 50px;

 }

If you want to put an SVG though, they react differently. You have to remove the attributes given in the SVG's code (you can open up an .svg file with most of the text editors). It's XML markup.

Read this answer for more info.

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