Ruby-on-rails 为什么图像没有显示在我的 rails 基本应用程序中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15827763/
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
Why images are not showing in my rails basic app
提问by swapnesh
my index.html.erbcode -
我的index.html.erb代码 -
<h1>Listing products</h1>
<table border="1">
<% @products.each do |product| %>
<tr>
<td><%= product.title %></td>
<td><%= product.description %></td>
<td><%= image_tag(product.image_url, :class => 'list_image') %></td>
<td><%= product.price %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Product', new_product_path %>
and images are under app\assets\images..but still images are not appearing on front end.
和图像在app\assets\images..但前端没有出现静止图像。
When I Firebugit, i believe image tag is correctly forming...let me know what i am missing in this part.
当我Firebug这样做时,我相信图像标签正在正确形成......让我知道我在这部分缺少什么。
<img src="/images/product1.jpg" class="list_image" alt="Product1">
Screenshot-
截图-


Images are in place as well -
图像也到位 -


Let me know what I am doing wrong and how do i fix it.
让我知道我做错了什么,我该如何解决。
EDIT
编辑
Let me know why it is not working in my case.
让我知道为什么它在我的情况下不起作用。
Although changing /images/product1.jpgTo/assets/product1.jpgmakes it working.
虽然改变/images/product1.jpgTo/assets/product1.jpg使它工作。
回答by Srikanth Jeeva
If you are using asset pipeline http://guides.rubyonrails.org/asset_pipeline.html,
如果您正在使用资产管道http://guides.rubyonrails.org/asset_pipeline.html,
The image path should be /assets/product1.jpginstead of /images/product1.jpg
图像路径应该是/assets/product1.jpg而不是/images/product1.jpg
If you are not using asset pipeline, move the images folder to public/images
如果您不使用资产管道,请将图像文件夹移动到 public/images
回答by rorra
I just checked your application, there is nothing wrong with your code. The only thing is to understand how image_tagworks.
我刚刚检查了您的应用程序,您的代码没有任何问题。唯一要做的就是了解image_tag 的工作原理。
Usually you put all your images, javscripts and stylesheests on the app/assetsdirectory. When you work on the development environment, those files as served uncompressed, but when you deploy to production, the assets are precompiled, minified, and the result files are stored in public/assets.
通常你把所有的图片、javscripts 和 stylesheets 放在app/assets目录中。当您在开发环境中工作时,这些文件是未压缩的,但是当您部署到生产环境时,资产被预编译、缩小,结果文件存储在public/assets 中。
The idea behind minified assets, is just to make the requests faster for the clients, and to save bandwidth.
缩小资产背后的想法只是让客户端的请求更快,并节省带宽。
Now, on the method image_tag, you can use an external path for the image, a local path for the image or a relative path for the image.
现在,在image_tag方法中,您可以使用图像的外部路径、图像的本地路径或图像的相对路径。
When you do
当你做
<%= image_tag "http://www.mywebsite.com/image.jpg" %>
it will use the absolute url for the image tag, and you will end with
它将使用图像标签的绝对网址,您将以
<img src="http://www.mywebsite.com/image.jpg" />
You can add a local path as well, like
您也可以添加本地路径,例如
<%= image_tag "/images/image.jpg" %>
Which will end in
哪个将结束
<img src="/images/image.jpg" />
which is actually the issue you are having, because rails, when it precompiles the files, it puts everything within /public/assets, and you can access those files by going to the path /assetsas the other users explained.
这实际上是您遇到的问题,因为 rails 在预编译文件时,会将所有内容都放在/public/assets 中,您可以按照其他用户的解释,通过转到路径/assets来访问这些文件。
So the code
所以代码
<%= image_tag "/assets/image.jpg" %>
actually works, because you end with
实际上有效,因为你以
<img src="/assets/image.jpg" />
The other thing you can do, is to use a relative path, i.e.
您可以做的另一件事是使用相对路径,即
<%= image_tag "image.jpg" %>
that will be converted to
这将被转换为
<img src="/assets/image.jpg" />
and that will work the same the last scenario.
这将与最后一个场景相同。
Nevertheless, on your application, you are going to let the users to upload their own images, this will happen later when you advance on the book, on a real world app, you will use a gem like paperclipor carrierwave
尽管如此,在您的应用程序中,您将让用户上传他们自己的图像,这将在您继续阅读书籍时发生,在现实世界的应用程序中,您将使用像回形针或载波这样的宝石
回答by surgentt
If you are received a file with a .jpeg extension try and renaming the file with just the ".jpg".
如果您收到带有 .jpeg 扩展名的文件,请尝试仅使用“.jpg”重命名该文件。
From
从
<%= image_tag "image.jpeg" %>
To:
到:
<%= image_tag "image.jpg" %>
回答by Peter de Ridder
As Srikanth already said, the assets path should be referenced. As an example you could put <%= image_tag 'rails.png' %>within your code and check firebug (or inspect element within chrome) to check the result.
正如 Srikanth 已经说过的,应该引用资产路径。例如,您可以放入<%= image_tag 'rails.png' %>代码并检查 firebug(或检查 chrome 中的元素)以检查结果。
I'm not quite sure why your code is not working, since I can see you followed Agile Web Development with Rails. I got the depot application running without problems. In your table I see you 'Product1', 'Product2' and 'Product3', is this what you actually filled in within the image_url text_field? What happens if you change 'Product1' to 'product1.jpg'?
我不太确定为什么您的代码不起作用,因为我可以看到您遵循使用 Rails 进行敏捷 Web 开发。我让仓库应用程序运行没有问题。在您的表格中,我看到您是“Product1”、“Product2”和“Product3”,这是您在 image_url text_field 中实际填写的内容吗?如果将“Product1”更改为“product1.jpg”会发生什么?
On a side note, if you want to use Paperclip, your call should look like this:
附带说明一下,如果您想使用 Paperclip,您的调用应如下所示:
<%= image_tag(product.image.url, class: 'list_image') %>

