Rails 3.1 图像的绝对 URL

发布于 2024-12-07 14:31:59 字数 292 浏览 0 评论 0原文

我正在使用 Rails 3.1。我正在尝试解决这个问题,令我惊讶的是,Rails 似乎根本没有提供这种方法。也许我错了。

谁能告诉我如何获得图像的完整绝对 URL?

我使用 asset_path(image.png) ,它为我提供了在应用程序中使用的相对路径。我尝试执行 root_url + asset_path(image.png) 但这只是给了我一个带有双斜杠的 http://localhost:3000//assets/image.png

有人有有效的方法吗?

I'm using Rails 3.1. I'm trying to figure this out, and to my surprise, it is starting to seem that rails does not come with this method at all. Maybe im wrong.

Can anyone show how I can get a full absolute URL to an image?

I use asset_path(image.png) which gives me the relative path to use within the app. I tried doing a root_url + asset_path(image.png) but that just gives me a http://localhost:3000//assets/image.png with the double slashes

Anyone have an efficient way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凌乱心跳 2024-12-14 14:31:59

See the Using asset hosts section in the documentation. You need to specify an asset_host. You can also construct it dynamically from the request chaining "#{request.protocol}#{request.host_with_port}"

压抑⊿情绪 2024-12-14 14:31:59

将其放入 application_helper.rb 中,

def asset_url asset
  "#{request.protocol}#{request.host_with_port}#{asset_path(asset)}"
end

然后您可以在视图中使用 asset_url

put this in application_helper.rb

def asset_url asset
  "#{request.protocol}#{request.host_with_port}#{asset_path(asset)}"
end

then you can use asset_url in your views.

御守 2024-12-14 14:31:59

对于 Rails 4,也许更早,请使用:

config.action_mailer.asset_host = 'https://assets.com'

per https://github.com/fphilipe/ premailer-rails/issues/16

For Rails 4, and maybe earlier, use:

config.action_mailer.asset_host = 'https://assets.com'

per https://github.com/fphilipe/premailer-rails/issues/16

如果没有你 2024-12-14 14:31:59

在我的 config/environments/*.rb 中,我已经为每个环境量身定制了这个:

config.domain = 'mysite.dev'

因此添加到每个文件很简单

config.action_controller.asset_host = "http://" + config.domain

。然后 asset_path 将奇迹般地表现得就像 asset_url 一样。

In my config/environments/*.rb I already have this tailored for each environment:

config.domain = 'mysite.dev'

So it was a simple matter of adding

config.action_controller.asset_host = "http://" + config.domain

to each file. Then asset_path will miraculously behave as if it were asset_url.

我要还你自由 2024-12-14 14:31:59

示例文件夹结构。

app/
   assets/
      flags/
         32x32/
            en.png
         256x256/
            en.png

如果您想生成绝对标志图像路径,我们可以在 ApplicationHelper 中添加两个方法:

module ApplicationHelper

  # Generate flag path by locale
  # - locale. Can be "en", "it", etc.
  # - flag_size. Will be used to set folder size. Folder size can be "32x32", "256x256".
  # Return flag image path. Path will absolute
  def generate_flag_path_by_locale(locale, folder_size = "32")
    folder = "#{flag_size}x#{flag_size}"
    domain_absolute_path = generate_domain_absolute_path
    flag_path = ("#{domain_absolute_path}/assets/flags/#{folder}/#{locale}.png")

    return flag_path
  end

  # Generate domain absolute path
  def generate_domain_absolute_path
    request_protocol = request.protocol
    request_host_with_port = request.host_with_port
    domain_absolute_path = request_protocol + request_host_with_port

    return domain_absolute_path
  end
end

在 apps/view/products.html.erb 中。我们只能调用:

<% flag_path = generate_flag_path_by_locale("en") %> 

最终结果:

http://my_domain.com:3000/资产/标志/32x32/en.png

Example folder structure.

app/
   assets/
      flags/
         32x32/
            en.png
         256x256/
            en.png

If you want to generate absolute flag image path we can add in to our ApplicationHelper two methods:

module ApplicationHelper

  # Generate flag path by locale
  # - locale. Can be "en", "it", etc.
  # - flag_size. Will be used to set folder size. Folder size can be "32x32", "256x256".
  # Return flag image path. Path will absolute
  def generate_flag_path_by_locale(locale, folder_size = "32")
    folder = "#{flag_size}x#{flag_size}"
    domain_absolute_path = generate_domain_absolute_path
    flag_path = ("#{domain_absolute_path}/assets/flags/#{folder}/#{locale}.png")

    return flag_path
  end

  # Generate domain absolute path
  def generate_domain_absolute_path
    request_protocol = request.protocol
    request_host_with_port = request.host_with_port
    domain_absolute_path = request_protocol + request_host_with_port

    return domain_absolute_path
  end
end

Into our apps/view/products.html.erb. We must to call only:

<% flag_path = generate_flag_path_by_locale("en") %> 

Final result:

http://my_domain.com:3000/assets/flags/32x32/en.png

离旧人 2024-12-14 14:31:59

您可以这样做:

root_url[0..-2] + asset_path(image.png)

...修剪根网址中的尾部斜杠吗?

Could you just do:

root_url[0..-2] + asset_path(image.png)

...to trim the trailing slash in the root url?

和我恋爱吧 2024-12-14 14:31:59

您需要使用“asset_url”而不是*asset_path*。

Bcz '_path' 总是返回相对路径,而 '_url' 将返回绝对 url。

You need to use 'asset_url' instead *asset_path*.

Bcz '_path' always return relative path and '_url' will return absolute url.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文