如何在Rails 3.0.x中使用Sprockets 2(如何使用预编译资产) [英] How to use Sprockets 2 with Rails 3.0.x (how to use precompiled assets)

查看:85
本文介绍了如何在Rails 3.0.x中使用Sprockets 2(如何使用预编译资产)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的rails 3.0应用程序中复制在rails 3.1中引入的资产管道的基础.

到目前为止,我有这样的事情: https://gist.github.com/1112393 .

So far, I've got something like this: https://gist.github.com/1112393.

效果很好:

  • 我的资产在app/assets/,lib/assets,vendor/assets ...
  • 他们都在/assets服务
  • 我可以使用链轮2提供的所有服务,等等...

问题是,我不希望Rails应用提供静态资产.服务器应该这样做. 如果我理解正确的话,这就是为什么可以在rails 3.1中预编译资产的原因. 因此,我已经完成了一个瑞克任务(使用Sprockets :: Environment的预编译方法).而且行得通,我的所有资产都位于/public/assets/.

The thing is, I don't want the rails app to serve static assets. The server should do it. That's why you can precompile assets in rails 3.1, if I understood correctly. So I've made a rake task that does just that (using the precompile method of Sprockets::Environment). And it works, I have all my assets at /public/assets/.

例如,我有

  • application-02f8c96b342b4569513d0edf39ef55eb.css
  • application-505e8f472350fb1e0d15f6ad2f5e0389.js
  • gallery-icons-0e922050a85718fef3cd570df4eb5845.png

但是在rails 3.1中,您可以在style.css.scss.erb中做类似的事情

But in rails 3.1, you can do something like that in your style.css.scss.erb

background: url(<%= asset_path("gallery-icons.png") %>)

您将获得

background: url(/assets/gallery-icons-0e922050a85718fef3cd570df4eb5845.png)

在预编译的文件中.

与stylesheet_link_tag和javascript_link_tag相同,如果我没记错的话,它们会在rails 3.1中被覆盖以添加哈希.

Same for stylesheet_link_tag, javascript_link_tag which are overwritten in rails 3.1 to add the hash, if I'm not mistaken.

我该怎么做?

给我您可以拥有的所有想法!谢谢.

Give me every idea you can have! Thanks.

推荐答案

Josh在这里回答了我: https ://github.com/sstephenson/sprockets/issues/151

Josh answered me here: https://github.com/sstephenson/sprockets/issues/151

Assets = Sprockets::Environment.new(Rails.root) do |env|
    assets =  ["javascripts", "stylesheets", "images", "fonts"]
    paths =   ["app/assets/", "lib/assets/", "vendor/assets/" ].map{|p| assets.map{|f| "#{p}#{f}" } }.flatten

    paths.each{ |path| env.append_path path }

    env.static_root = Rails.root.join("public", "assets")
end

所以基本上,我有一个rake任务可以对资产进行预编译:

So basically, I have a rake task to precompile the assets:

namespace :assets do
    task :precompile => :environment do
        Assets.precompile(*Rails.application.config.assets.precompile)
    end
end

我的问题主要是想知道如何请求这些资产. 答案很简单:

My problem was mainly to know how to request these assets. The answer is quite simple:

Assets['application.js'].digest

具有指纹,可以轻松获取文件名.

Having the fingerprint, it's easy to get the filename.

我创建了包括以下资产的助手:sprockets_include_tagsprockets_image_tag.

I created helpers to include these assets: sprockets_include_tag and sprockets_image_tag.

完成交易.

(尽管现在,我不能在样式表(style.css.scss.erb)中使用这些帮助器)

(Although right now, I can't use these helpers in my stylesheets (style.css.scss.erb))

这篇关于如何在Rails 3.0.x中使用Sprockets 2(如何使用预编译资产)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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