【问题标题】:Rails 3.1 absolute URL to an imageRails 3.1 图像的绝对 URL
【发布时间】:2011-09-29 12:11:33
【问题描述】:

我使用的是 Rails 3.1。我试图弄清楚这一点,令我惊讶的是,似乎 Rails 根本没有这种方法。也许我错了。

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

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

谁有有效的方法?

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-3.1 asset-pipeline


【解决方案1】:

请参阅documentation 中的使用资产主机部分。您需要指定asset_host。也可以从请求链中动态构造"#{request.protocol}#{request.host_with_port}"

【讨论】:

  • 似乎assets_host 适用于asset_path 的所有调用,我只需要一次图像的绝对URL。我会查看请求链
  • 使用"#{request.protocol}#{request.host_with_port}#{asset_path("image.png")}"
  • 使用#{request.protocol}#{request.host_with_port}#{asset_path("image.png")}时要小心。如果您在应用程序配置中设置了config.action_controller.asset_host 选项,那么asset_path 将返回绝对url,您将得到类似http://example.comhttp://cdn.example.com/assets/image.png 的内容。
  • Simone,请注意在 Rails 3 中不存在asset_url!我只是浪费了 10 分钟研究它。
  • 应该在 Rails 3 中,这个问题是在引用 Rails 3.1。
【解决方案2】:

把这个放到application_helper.rb

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

那么你可以在你的视图中使用asset_url

【讨论】:

  • 这将返回我当前所在的子域
【解决方案3】:

对于 Rails 4,或许更早的版本,使用:

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

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

【讨论】:

  • 效果很好!谢谢
  • 实际上(至少在 rails4+ 中)你想使用 config.asset_host 所以它被设置为所有子系统而不仅仅是动作邮件。
【解决方案4】:

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

config.domain = 'mysite.dev'

所以这是一个简单的添加问题

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

到每个文件。然后asset_path 会奇迹般地表现得就像是asset_url

【讨论】:

  • 您甚至不需要将协议放在那里,只需配置asset_host 应该使asset_path 添加协议和主机名前缀。
  • 我发现这个 rails 行为错误,asset_path 应该是路径 asset_url 应该是 url。如果有 asset_host =/,我真的不喜欢 rails 改变asset_path 行为的方式
【解决方案5】:

示例文件夹结构。

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/assets/flags/32x32/en.png

【讨论】:

    【解决方案6】:

    你能做到吗:

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

    ...修剪根 url 中的尾部斜杠?

    【讨论】:

      【解决方案7】:

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

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

      【讨论】:

      • 已经试过了。没有asset_url这样的方法。这就是我得到的undefined method asset_url'`
      • 请试试这个 ActionController::Base.asset_host = "assets.example.com" 或查看链接api.rubyonrails.org/classes/ActionView/Helpers/…
      • 似乎assets_host适用于asset_path的所有调用,我只需要一次图像的绝对url。
      • 如果你只想要一次,那么在这种情况下你可以使用你自己的自定义帮助方法。
      • asset_url 在 CSS 中不存在,就像 _path 与 _url 命名约定在帮助程序中的其他地方一样。标题为 asset-url 的 CSS 帮助器(注意破折号而不是下划线)在相同的约定中不起作用,而是将 url('image.png') 框架附加到 CSS 数据。
      猜你喜欢
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多