ruby-on-rails – 如何將CSS / JS檔案打包在沒有生成器的Rails 3.1中?
我想寫一個寶石,一旦繫結到Rails 3.1 Gemfile,就會包含一些樣板CSS和JS檔案.
我知道生成器,但是如果沒有它們可以做到這一點,那麼可以新增和刪除寶石的效果,而不必在Rails Gemfile中編輯一行呢?
理想情況下,我希望寶石在安裝時包含其預設的CSS / JS,然後讓使用者使用生成器生成CSS / JS檔案,如果他們想進行任何修改.
這是一個從ofollow,noindex" target="_blank">jquery-rails 複製的示例gem,其中包含沒有生成器的javascript檔案.
css_gem/ lib/ css_gem.rb{1} css_gem/ engine.rb{2} app/ assets/ stylesheets/ css_gem/ index.css{3} base.css{4}
{1} lib / css_gem.rb
module CssGem require "css_gem/engine" end
{2} lib / css_gem / engine.rb
module CSSGem class Engine < Rails::Engine end end
{3} app / assets / stylesheets / css_gem / index.css
/* *= require base */
{4} app / assets / stylesheets / css_gem / base.css
.custom { color: red; }
Rails Gemfile
gem 'css_gem', :path => 'path_to_my_local_gem'
這對我來說不起作用,Rails看不到CSS檔案.我究竟做錯了什麼?
解決方案:幸運的是,我發現一個視訊握住我的手:http://house9.blogspot.com/2011/06/rails-31-asset-gem.html
我不得不手動新增* = require css_gem到我的Rails樣式表清單(app / assets / stylesheets / application.css).咄.
免責宣告 – 我還沒有嘗試過.您可以利用Rails 3.1中的資產管道,將寶石中的資產用於客戶端應用程式,而無需使用生成器.
還沒有找到這個使用的實際例子,但這裡是早期文件的連結
http://edgeguides.rubyonrails.org/asset_pipeline.html
http://edgeguides.rubyonrails.org/asset_pipeline.html#adding-assets-to-your-gems
http://stackoverflow.com/questions/6726256/how-would-you-package-css-js-files-in-a-gem-for-rails-3-1-without-generators