Rails 3.1: Determine if asset exists

后端 未结 4 808
爱一瞬间的悲伤
爱一瞬间的悲伤 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:31

    Given an image in app/assets/images/lolshirts/theme/bg-header.png,

    Rails.application.assets.find_asset 'lolshirts/theme/bg-header.png'
     => #> Sprockets::StaticAsset:0x80c388ec pathname="/Users/joevandyk/projects/tanga/sites/lolshirts/app/assets/images/lolshirts/theme/bg-header.png", mtime=2011-10-07 12:34:48 -0700, digest="a63cc84aca38e2172ae25de3d837c71a">
    
    Rails.application.assets.find_asset 'notthere.png'
     => nil
    

提交回复
热议问题