简体   繁体   English

无法加载Ruby on Rails 4资产图像

[英]Ruby on rails 4 assets images cannot be loaded

Currently, I am using ruby on rails 4. 目前,我正在使用Rails 4上的ruby。

In the html part, I am trying to load an image: 在html部分中,我正在尝试加载图像:

<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/ 图像文件:logo_lg.svg位于以下位置:app / assets / images / flatty /

I tried: 我试过了:

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

And in production.rb file: config.serve_static_assets = true . 在production.rb文件中: config.serve_static_assets = true

Moreover, tried: rails assets:precompile 此外,尝试: rails assets:precompile

But the system still cannot find the image file. 但是系统仍然找不到图像文件。

You have a .svg but you are trying to load a .png . 您有一个.svg但您正在尝试加载.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: 只需将它们放在正确的位置(即images目录)并写下:

<%= 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): 此外,如果您要为图像设置样式,则可以为其指定一个类或一个id(id更适合徽标):

<%= 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: 然后只需将CSS放入您的应用程序css文件或由页面控制器生成的CSS文件中即可:

  .someClass {

  width: 50px;
  height: 50px;

 }


 #someId {

  width: 50px;
  height: 50px;

 }

If you want to put an SVG though, they react differently. 如果您想放置SVG,它们的反应会有所不同。 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). 您必须删除SVG代码中提供的属性(可以使用大多数文本编辑器打开.svg文件)。 It's XML markup. 这是XML标记。

Read this answer for more info. 阅读此答案以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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