将图像添加到 ruby on rails 中的布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1814736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add image to layout in ruby on rails
提问by Erika
I would like to add an image in my template for my ruby on rails project where i currenly have the code <img src="../../../public/images/rss.jpg" alt="rss feed" />in a the layout stores.html.erbfile however this doesn't seem to load as it looks like its missing a route which i'm not sure what its supposed to be.
我想在我的 ruby on rails 项目的模板中添加一个图像,其中我目前<img src="../../../public/images/rss.jpg" alt="rss feed" />在布局stores.html.erb文件中有代码,但是这似乎没有加载,因为它看起来像是缺少一条我不确定的路线它应该是。
Any ideas please?
请问有什么想法吗?
回答by Doug Neiner
Anything in the publicfolder is accessible at the root path (/) so change your img tag to read:
public文件夹中的任何内容都可以在根路径 ( /) 中访问,因此将您的 img 标签更改为:
<img src="/images/rss.jpg" alt="rss feed" />
If you wanted to use a rails tag, use this:
如果您想使用 rails 标签,请使用以下命令:
<%= image_tag("rss.jpg", :alt => "rss feed") %>
回答by scottd
In a Ruby on Rails project by default the root of the HTML source for the server is the public directory. So your link would be:
默认情况下,在 Ruby on Rails 项目中,服务器的 HTML 源代码的根目录是公共目录。所以你的链接是:
<img src="images/rss.jpg" alt="rss feed" />
But it is best practice in a Rails project to use the built in helper:
但在 Rails 项目中使用内置帮助程序是最佳实践:
<%= image_tag("rss.jpg", :alt => "rss feed") %>
That will create the correct image link plus if you ever add assert servers, etc it will work with those.
这将创建正确的图像链接,此外,如果您添加断言服务器等,它将与这些服务器一起使用。
回答by Agung Kessawa
When using the new ruby, the image folder will go to asset folder on folder app
使用新的 ruby 时,图像文件夹将转到文件夹应用程序上的资产文件夹
after placing your images in image folder, use
将图像放入图像文件夹后,使用
<%=image_tag("example_image.png", alt: "Example Image")%>
回答by Palermo Andre Deschamps
simple just use the img tag helper. Rails knows to look in the images folder in the asset pipeline, you can use it like this
简单只需使用 img 标签助手。Rails 知道查看资源管道中的图像文件夹,您可以像这样使用它
<%= image_tag "image.jpg" %>
回答by Artem Baranov
It's working for me:
它对我有用:
<%= image_tag( root_url + "images/rss.jpg", size: "50x50", :alt => "rss feed") -%>
<%= image_tag( root_url + "images/rss.jpg", size: "50x50", :alt => "rss feed") -%>
回答by Alonso Huayta Laura
image_tag is the best way to do the job friend
image_tag 是完成这项工作的最佳方式朋友

