12

In Rails 5.1, there is a deprecation warning if we use asset_path for files in public folder.

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.

So I tried public_asset_path, but it is not working, is there a helper?

Nicolas Maloeuvre
  • 3,069
  • 24
  • 42

3 Answers3

22

If want to use helper method without asset pipeline, need to specify the skip_pipeline option.

Like this.

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

Or if want to enable asset fallback for the entire application, config.assets.unknown_asset_fallback can be set to true. See: http://guides.rubyonrails.org/asset_pipeline.html#raise-an-error-when-an-asset-is-not-found

John Smith
  • 6,105
  • 16
  • 58
  • 109
  • In case someone may need alternative full-form asset_path version: `ActionController::Base.helpers.asset_path('favicon.ico', host: "https://example.com", skip_pipeline: true)` – BinaryButterfly Nov 13 '22 at 11:50
-1

you can use Rails.public_path to get the public directory and then find the file from there, you could also use #{RAILS_ROOT}/public they would both do the same thing.

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

Community
  • 1
  • 1
djlazz3
  • 1
  • 2
-3

So I added this method in helpers/application_helper.rb

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

and then I can use public_path('images/image.jpg') in views

Nicolas Maloeuvre
  • 3,069
  • 24
  • 42