Rails-heroku资产路径中图像的绝对URL [英] Rails - absolute URL for images in assets path for heroku

查看:81
本文介绍了Rails-heroku资产路径中图像的绝对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在app/assets/images/admin目录下有图片.

在开发中,我使用了类似的方法来获取URL

In development I've used something like this to get the URL

"#{root_url}assets/admin/filename.jpg"

但是它不适用于Heroku.

But its not working on Heroku.

那么在Heroku上引用资源文件夹下的图像的最佳方法是什么?

So what is the best way to reference images under assets folder on Heroku?

推荐答案

资产文件夹下的参考图像

reference images under assets folder

如果您使用的是 image_tag ,无论您处于production还是development,您都可以调用相对路径:

If you're using image_tag, you'll be able to call the relative path regardless of whether you're in production or development:

<%= image_tag "admin/filename.jpg" %>

-

如果要在 CSS 中调用图像,则需要使用

If you're calling the image in your CSS, you'll need to use asset-path or asset-url:

#app/assets/stylesheets/application.css
.header {
   background: asset-url("admin/filename.png");
}


您遇到的问题与资产指纹.

预编译(在Heroku中是必需的)资产时,所有imagescssjavascript等都将放入public/assets文件夹中.更重要的是,每个文件都带有指纹(带有一些数字):

When you precompile your assets (which is required in Heroku), all the images, css, javascript etc is put into the public/assets folder. More importantly, each file is fingerprinted (has some numbers appended to it):

global-908e25f4bf641868d8683022a5b62f54.css

因此,在您处于生产"状态时,调用#{root_url}assets/admin/filename.jpg会导致 404 ,因为该文件将不存在.

Thus, calling #{root_url}assets/admin/filename.jpg will result in a 404 when you're in "production", because the file will not exist.

您需要使用路径助手之一(上述)来确保Rails可以挑选正确的文件,而不管您使用的是哪种环境.

You need to use one of the path helpers (above) to make sure Rails can pick out the correct file, regardless of which environment you're using.

这篇关于Rails-heroku资产路径中图像的绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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