Skip to content
Xu Tao edited this page Oct 5, 2013 · 4 revisions

静态文件处理

常用目录

app/assets app/assets/javascripts app/assets/stylesheets app/assets/images lib/assets vendor/assets

常用方法

stylesheet_link_tag javascript_include_tag image_tag asset_path asset_data_url require require_tree

例子

<%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> <%= image_tag "rails.png" %>

application.css.erb

.class { background-image: url(<%= asset_path 'image.png' %>) } #logo { background: url(<%= asset_data_uri 'logo.png' %>) }

// ...
//= require jquery
//= require jquery_ujs
//= require_tree .

Asset Organization

  1. Pipeline assets can be placed inside an application in one of three locations: app/assets, lib/assets or vendor/assets.

  2. app/assets is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.

  3. lib/assets is for your own libraries' code that doesn't really fit into the scope of the application or those libraries which are shared across applications.

  4. vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks.

Search Paths

  1. When a file is referenced from a manifest or a helper, Sprockets searches the three default asset locations for it.

  2. The default locations are: app/assets/images and the subdirectories javascripts and stylesheets in all three asset locations, but these subdirectories are not special. Any path under assets/* will be searched.

常用Gem

sprockets

https://github.com/sstephenson/sprockets Rack-based asset packaging system CoffeeScript, Sass, SCSS and LESS.

kaminari

https://github.com/amatsuda/kaminari A Scope & Engine based, clean, powerful, customizable and sophisticated paginator for Rails 3

入门资料

http://guides.rubyonrails.org/layouts_and_rendering.html http://guides.rubyonrails.org/security.html http://guides.rubyonrails.org/working_with_javascript_in_rails.html http://guides.rubyonrails.org/asset_pipeline.html http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html