Ruby-on-rails 如何在 Rails 3.1 中获取资产的绝对 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6500025/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:28:37  来源:igfitidea点击:

How do I get an absolute URL for an asset in Rails 3.1?

ruby-on-railsruby-on-rails-3

提问by Steven

image_pathreturns a path only (no host).

image_path仅返回路径(无主机)。

The url_forhelper won't accept a single path, so something like url_for(image_path('image.png'))wouldn't work. While the internal url_forin ActionDispatch::Http::Url.url_forwould appear to do the trick (source), there doesn't seem to be a public interface for it.

url_for助手将不会接受一个单一的路径,所以像url_for(image_path('image.png'))行不通的。虽然内部url_forinActionDispatch::Http::Url.url_for似乎可以解决问题(source),但似乎没有公共接口。

How should I go about doing it? Ultimately, it'd be nice to have a function like image_urlthat works like url_fordoes for routes so that I could call image_url('image.png')and get the absolute URL given all of the default_url_options.

我该怎么做?最终,有一个像路由一样image_url工作的函数会很好,url_for这样我就可以调用image_url('image.png')并获取给定所有default_url_options.

采纳答案by Steven

It would appear that as of recently, sass-railsnow interprets the image_urlcommand within a scss file in the expected manner, resolving to the final location of the image in question.

从最近看来,sass-rails现在以image_url预期的方式解释scss 文件中的命令,解析到有问题的图像的最终位置。

回答by tig

def image_url(source)
  URI.join(root_url, image_path(source))
end

This way you get url either using assets_hostor joining with root_url.

通过这种方式,您可以使用assets_host或加入 root_url来获取 url 。

回答by JofoCodin

Try this in your application_helper.rb(from one of the comments on the page Spike listed):

在您的application_helper.rb(来自 Spike 列出的页面上的评论之一)中尝试此操作:

def image_url(source)
  "#{root_url}#{image_path(source)}"
end

回答by courtsimas

Our production and staging assets are on s3/cloudfront... but not locally/dev. So I wrote this (may be overkill, and probably can be simplified):

我们的生产和暂存资产位于 s3/cloudfront...但不在本地/dev。所以我写了这个(可能有点矫枉过正,可能可以简化):

  def get_digest_file(source)
     return asset_path(source.to_s.downcase) unless Rails.application.config.assets.digests.present?
     return ActionController::Base.asset_host + "/assets/" + Rails.application.config.assets.digests[source.to_s.downcase]
  end

回答by Hezad

From Full url for an image-path in Rails 3

来自Rails 3 中图像路径的完整 url

request.protocol + request.host_with_port + image_path('image.png')

You can even create a helper to DRY it, something like

你甚至可以创建一个帮手来干燥它,比如

def full_image_path(img_path)
    request.protocol + request.host_with_port + image_path(img_path)
end

回答by Lalit Kumar Maurya

You can define css with absolute url for any background image or other assets. So you can generate it dynamically following this How to get absolute image paths in your css files using sass/scss.

您可以为任何背景图像或其他资产定义带有绝对 url 的 css。因此,您可以按照如何使用 sass/scss 在 css 文件中获取绝对图像路径来动态生成它。

CSS

CSS

body {
  background-image: image-url(background.png);
}

In your environment file change it with yours

在您的环境文件中将其更改为您的

config.action_controller.asset_host = "http://your.domain.com/"

Then your css will look something like that:

然后你的 css 看起来像这样:

body {
  background-image: url(http://your.domain.com/assets/background.png);
}

回答by Michael Wiltbank

If you are having an issue with not being able to reach root_urlfrom a view, you can either pass it to the view, or you could also add an .envvariable and then call it in the root.

如果您遇到无法root_url从视图访问的问题,您可以将它传递给视图,或者您也可以添加一个.env变量,然后在根中调用它。

asset_url('image.png', host: "https://#{ENV['HOSTNAME']}/")