问题描述
我正在尝试使用image_tag
显示徽标图像,但是它总是作为损坏的图像出现.
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") %>
我在/assets/images/logo.png
和/assets/logo.png
中都具有"logo.png",并尝试了许多URL变体,例如images/logo.png
,/images/logo.png
,assets/images/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?
谢谢.
修改:添加信息
我正在将rails ~> 5.0.1
与bootstrap-sass ~> 3.3.6
一起使用.
文件位于/project_base/app/assets/images/logo.png
.
运行rake assets:clobber & rake assets:precompie
似乎没有任何改变.
Running rake assets:clobber & rake assets:precompie
doesn't seem to change anything.
在/project_base/public/...
中没有为logo.png
生成的资产.
There is no generated asset for logo.png
in /project_base/public/...
.
我没有更改development.rb
文件中的任何内容,但尝试添加:
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.
推荐答案
您只需要将图像放在资产/图像/中,就可以从任何视图加载图像,方法是
You just need to put your image in assets/images/ and you can load it from any view by
<%=image_tag("logo.png")%>
如果rails找不到您的图片,那么它将抛出异常 AssetNotFound
if rails fails to find your image then it will throw an exception AssetNotFound
这篇关于Ruby on Rails-image_tag无法找到本地图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!