Rails 5.1:公共文件夹中文件的url帮助程序

Nic*_*vre 8 ruby-on-rails actionview asset-pipeline

在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,但它不起作用,有帮手吗?

Joh*_*ith 16

如果想要使用没有资产管道的helper方法,需要指定skip_pipeline选项.

像这样.

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

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


Nic*_*vre -3

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

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

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