Rails Rails 官方文档,有疑惑 之 “image_url”

hiveer · 2018年04月02日 · 最后由 hiveer 回复于 2018年04月02日 · 1752 次阅读

我对下面的这个文档产生了疑惑:

Computes the full URL to an image asset. This will use image_path internally, so most of their behaviors will be the same. Since image_url is based on asset_url method you can set :host options. If :host options is set, it overwrites global config.action_controller.asset_host setting.

image_url "edit.png", host: "http://stage.example.com" # => http://stage.example.com/edit.png

以上的文档是基于我们要使用 asset pipleline 的。我们先看下源码的调用:

image_url("edit.png", host: "http://stage.example.com") =>  
url_to_asset("edit.png", {type: :image, host: "http://stage.example.com")} =>  
path_to_asset("edit.png", {type: :image, host: "http://stage.example.com", protocol: :request)}

path_to_asset中,参考源码链接https://github.com/rails/rails/blob/3387676efdd03fd6e5b9a70b215ce02cdb1d4ee1/actionview/lib/action_view/helpers/asset_url_helper.rb#L183

我们重点关注计算 path 的那部分:

if source[0] != ?/
  if options[:skip_pipeline]
    source = public_compute_asset_path(source, options)
  else
    source = compute_asset_path(source, options)
  end
end

因为我们并没有 skip pipeline,所以自然会去执行 else 中的代码 compute_asset_path(source, options)
compute_asset_path是被 sprockets-rails 重写了的,源码在这里 (https://github.com/rails/sprockets-rails/blob/857e781998c10e4f429699da1d47ef251844991f/lib/sprockets/rails/helper.rb#L77)

我们可以看到compute_asset_path会到所有的 asset 中去找这个 source("edit.png"),如果找到了就加上 assets_prefix

File.join(assets_prefix || "/", legacy_debug_path(asset_path, debug))

如果没找到就报错。

所以,我们这里假设“edit.png"是存在项目中的。所以,根据代码的推算我们得到的结果应该是:
"http://stage.example.com/assets/edit-fingerprint.png"
而不是“http://stage.example.com/edit.png

实验

我在我的一个现有项目 (Rails5) 做了一个实验:

  1. 我添加了一个 image('app/assets/images/approve.jpg')
  2. 在任何一个 view 当中 raise error(<% raise %>),这是为了拿到运行时
  3. 刷新 view 对应的页面,然后出现了错误调试
  4. 测试结果 ruby >> image_url 'approve.jpg', host: "http://stage.example.com" => "http://stage.example.com/assets/approve-53ea399216b1c6b2a08f6da00032588a01581600317d94d0445d3514f1dbdba9.jpg"

可以看到,结果和我对代码的推算一致,但是和文档却不同。这个是不是说明文档有问题呢?

文档地址是?

应该是文档错了,相对地址会加上 /assets 前缀。

@Rei 嗯,谢谢回复啦。我下来给文档提个 change request。

在预发布版本里面,这个问题已经被修复了。所以目前这个问题只存在于 stable 的版本文档。 https://github.com/rails/rails/blob/db7edd81062648281d1e50c8ff9ebfafac5a9c3d/actionview/lib/action_view/helpers/asset_url_helper.rb#L384

hiveer 关闭了讨论。 04月02日 16:58
hiveer 重新开启了讨论。 04月02日 16:59
需要 登录 后方可回复, 如果你还没有账号请 注册新账号