Rails 3.1: Determine if asset exists

后端 未结 4 805
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 00:42

Is there a built-in way to determine if an asset exists without resorting to File.exists?(File.join(Rails.root, \"foo\", \"bar\", \"baz\")) and that looks throu

4条回答
  •  一向
    一向 (楼主)
    2020-12-24 01:21

    Since this is still the top question when searching Google, and since the accepted answer does not work properly in production (at least in some cases), here is the solution that works for me (Rails 4.2.5.1):

    def asset_exist?(path)
      if Rails.configuration.assets.compile
        Rails.application.precompiled_assets.include? path
      else
        Rails.application.assets_manifest.assets[path].present?
      end
    end
    

    This is copied from this github issue

提交回复
热议问题