【问题标题】:Rails 5.1 : url helper for files in public folderRails 5.1:公共文件夹中文件的 url 助手
【发布时间】:2017-10-17 21:29:32
【问题描述】:

在 Rails 5.1 中,如果我们对公用文件夹中的文件使用asset_path,则会出现弃用警告。

DEPRECATION WARNING: The asset "favicon.ico" is not present in the asset pipeline.Falling back to an asset that may be in the public folder. This behavior is deprecated and will be removed.

所以我尝试了public_asset_path,但还是不行,有没有帮手?

【问题讨论】:

  • 我在引用外部资源时遇到了同样的错误,即:aws 或其他任何地方的图像
  • 您找到任何解决方案了吗?
  • 没有 ????,还在等待
  • 好吧,我找到了我的,问题在于 URL,URL 被附加了另一个 https: 虽然它已经有 http:// 所以完整的 URL 就像 https:xyz.abc 这显然是不对。修复我的应用程序中的 URL 生成器解决了这个问题。在一些地方,我的文件路径完全错误,即路径甚至不是路径,由于这个警告,我也修复了这个问题。 :)
  • @NicolasMaloeuvre 你找到解决办法了吗?

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


【解决方案1】:

如果想使用无资产管道的辅助方法,需要指定skip_pipeline选项。

像这样。

image_path("user/favicon.png", skip_pipeline: true)

或者,如果想要为整个应用程序启用资产回退, config.assets.unknown_asset_fallback 可以设置为真。 见:http://guides.rubyonrails.org/asset_pipeline.html#raise-an-error-when-an-asset-is-not-found

【讨论】:

    【解决方案2】:

    您可以使用Rails.public_path 获取公共目录,然后从那里找到文件,您也可以使用#{RAILS_ROOT}/public,它们都会做同样的事情。

    来源:Getting absolute path to the file inside the public folder in Rails

    【讨论】:

      【解决方案3】:

      所以我在 helpers/application_helper.rb 中添加了这个方法

      module ApplicationHelper
        def public_path(path)
            "#{ Rails.env.development? ? 'http://localhost:3000/' : 'https://cdn.mysite.fr/' }#{ path }"
        end
      end
      

      然后我可以在视图中使用public_path('images/image.jpg')

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        • 2021-09-08
        • 2016-07-02
        • 1970-01-01
        • 2011-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多