Ruby on Rails-image_tag无法找到本地图像 [英] Ruby on Rails - image_tag cannot find local image

查看:133
本文介绍了Ruby on Rails-image_tag无法找到本地图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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.pngassets/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.1bootstrap-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无法找到本地图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆