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

查看:60
本文介绍了如何从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.

这是我曾经做过的事情:

Here's what I used to do:

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环境中检索资产.向其传递逻辑路径,您将获得一个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天全站免登陆