How does one reference compiled assets from the controller in Rails 3.1?

后端 未结 6 1373
鱼传尺愫
鱼传尺愫 2020-12-09 03:50

I\'m using the PDFkit in my controller to build out a series of PDFs, zip them up, and then send them to the user.

In order to control the output styles, I tell PDFK

6条回答
  •  忘掉有多难
    2020-12-09 04:11

    Rails.application.assets is poorly documented but it provides access to Rails' hook into Sprockets, as a Sprockets::Environment object. Rails uses Sprockets to basically run the whole asset pipeline, and this is where you should hook in for things like this:

    kit.stylesheets << Rails.application.assets['application.css'].pathname
    

    https://github.com/sstephenson/sprockets says of it:

    Accessing Assets Programmatically

    You can use the find_asset method (aliased as []) to retrieve an asset from a Sprockets environment. Pass it a logical path and you'll get a Sprockets::BundledAsset instance back:

      environment['application.js']
      # => #
    

    Call to_s on the resulting asset to access its contents, length to get its length in bytes, mtime to query its last-modified time, and pathname to get its full path on the filesystem.

提交回复
热议问题