一个引用如何从 Rails 3.1 中的控制器编译资产? [英] How does one reference compiled assets from the controller in Rails 3.1?

查看:29
本文介绍了一个引用如何从 Rails 3.1 中的控制器编译资产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中使用 PDFkit 来构建一系列 PDF,将它们压缩,然后将它们发送给用户.

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.

为了控制输出样式,我告诉 PDFKit 在内容生成期间使用哪些样式表.我需要传递 CSS 文件的文件引用.由于 Rails 现在正在编译和重命名我的样式表,我不确定如何在我的控制器中引用已编译的 CSS 资产.

In order to control the output styles, I tell PDFKit which stylesheets to use during content generation. I need to pass along the file reference of the CSS file. Since Rails is now compiling and renaming my stylesheets, I'm not sure how to reference the compiled CSS asset inside my controller.

这是我过去常做的:

InvoicesController < ApplicationController
  def download
    kit = PDFKit.new(render_to_string(:show, :layout => false))
    kit.stylesheets << "#{Sass::Plugin.options[:css_location]}/application.css"
    kit.to_file("#{file_date_string}.pdf")
    # snip
  end
end

Sass::Plugin.options[:css_location] 现在返回错误的位置,更不用说 application.css 不再是文件的有效名称.我应该提到我有一个 app/assets/application.css 文件,它用作我的 SCSS 文件的清单,并且它通过 stylesheet_link_tag() 方法在我的视图中正常工作.

Sass::Plugin.options[:css_location] now returns the incorrect location, not to mention the fact that application.css is no longer the valid name of the file. I should mention that I have an app/assets/application.css file that serves as a manifest for my SCSS files, and it is working correctly in my views via the stylesheet_link_tag() method.

基本上,我正在寻找一个与 asset_path() 等效的控制器,以便执行以下操作:

Basically what I'm looking for is a controller equivalent of asset_path() in order to do something like this:

kit = PDFKit.new(render_to_string(:show, :layout => false))
kit.stylesheets << asset_path('application.css')
kit.to_file("#{file_date_string}.pdf")

有人可以帮忙吗?

推荐答案

Rails.application.assets 的文档很差,但它提供了对 Rails 挂钩到 Sprockets 的访问,作为 Sprockets::Environment 对象.Rails 使用 Sprockets 来运行整个资产管道,这是您应该挂钩的地方:

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 说到:

以编程方式访问资产

您可以使用 find_asset 方法(别名为 [])从链轮环境中检索资产.向它传递一个逻辑路径,你会得到一个 Sprockets::BundledAsset 实例:

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']
  # => #<Sprockets::BundledAsset ...>

在生成的资产上调用 to_s 以访问其内容,length 以字节为单位获取其长度,mtime 以查询其最后修改时间时间和 pathname 以获取其在文件系统上的完整路径.

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.

这篇关于一个引用如何从 Rails 3.1 中的控制器编译资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆