rails 4.0.13 → 4.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +20 -15
- data/guides/CHANGELOG.md +5 -74
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/getting_started/challenge.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/assets/images/getting_started/new_post.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/icons/README +1 -1
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails4_features.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +30 -34
- data/guides/assets/stylesheets/main.css +2 -1
- data/guides/assets/stylesheets/print.css +1 -1
- data/guides/bug_report_templates/action_controller_gem.rb +2 -0
- data/guides/bug_report_templates/action_controller_master.rb +2 -0
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- data/guides/bug_report_templates/active_record_master.rb +2 -1
- data/guides/code/getting_started/Gemfile +1 -1
- data/guides/code/getting_started/app/assets/javascripts/application.js +1 -2
- data/guides/code/getting_started/config/environments/development.rb +1 -1
- data/guides/code/getting_started/public/404.html +2 -0
- data/guides/code/getting_started/public/422.html +2 -0
- data/guides/code/getting_started/public/500.html +2 -0
- data/guides/rails_guides/helpers.rb +1 -1
- data/guides/source/2_2_release_notes.md +2 -2
- data/guides/source/2_3_release_notes.md +8 -8
- data/guides/source/3_0_release_notes.md +1 -2
- data/guides/source/3_1_release_notes.md +1 -1
- data/guides/source/3_2_release_notes.md +12 -12
- data/guides/source/4_0_release_notes.md +79 -46
- data/guides/source/4_1_release_notes.md +601 -0
- data/guides/source/_welcome.html.erb +1 -1
- data/guides/source/action_controller_overview.md +117 -31
- data/guides/source/action_mailer_basics.md +19 -19
- data/guides/source/action_view_overview.md +131 -12
- data/guides/source/active_model_basics.md +6 -6
- data/guides/source/active_record_basics.md +15 -15
- data/guides/source/active_record_callbacks.md +18 -16
- data/guides/source/active_record_querying.md +67 -39
- data/guides/source/active_record_validations.md +31 -31
- data/guides/source/active_support_core_extensions.md +63 -74
- data/guides/source/active_support_instrumentation.md +13 -4
- data/guides/source/api_documentation_guidelines.md +19 -5
- data/guides/source/asset_pipeline.md +544 -249
- data/guides/source/association_basics.md +81 -22
- data/guides/source/caching_with_rails.md +15 -6
- data/guides/source/command_line.md +28 -19
- data/guides/source/configuring.md +98 -50
- data/guides/source/contributing_to_ruby_on_rails.md +11 -11
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +36 -5
- data/guides/source/development_dependencies_install.md +89 -8
- data/guides/source/documents.yaml +7 -1
- data/guides/source/engines.md +648 -224
- data/guides/source/form_helpers.md +56 -45
- data/guides/source/generators.md +7 -3
- data/guides/source/getting_started.md +379 -164
- data/guides/source/i18n.md +59 -23
- data/guides/source/index.html.erb +1 -1
- data/guides/source/initialization.md +153 -56
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +3 -3
- data/guides/source/layouts_and_rendering.md +12 -11
- data/guides/source/maintenance_policy.md +4 -23
- data/guides/source/migrations.md +41 -37
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +27 -23
- data/guides/source/rails_application_templates.md +25 -6
- data/guides/source/rails_on_rack.md +35 -51
- data/guides/source/routing.md +108 -99
- data/guides/source/ruby_on_rails_guides_guidelines.md +2 -2
- data/guides/source/security.md +33 -31
- data/guides/source/testing.md +37 -34
- data/guides/source/upgrading_ruby_on_rails.md +335 -16
- data/guides/source/working_with_javascript_in_rails.md +18 -10
- metadata +66 -39
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/source/kindle/KINDLE.md +0 -26
@@ -39,7 +39,7 @@ Action Controller
|
|
39
39
|
|
40
40
|
```ruby
|
41
41
|
{
|
42
|
-
key: 'posts/1-
|
42
|
+
key: 'posts/1-dashboard-view'
|
43
43
|
}
|
44
44
|
```
|
45
45
|
|
@@ -51,7 +51,7 @@ Action Controller
|
|
51
51
|
|
52
52
|
```ruby
|
53
53
|
{
|
54
|
-
key: 'posts/1-
|
54
|
+
key: 'posts/1-dashboard-view'
|
55
55
|
}
|
56
56
|
```
|
57
57
|
|
@@ -63,7 +63,7 @@ Action Controller
|
|
63
63
|
|
64
64
|
```ruby
|
65
65
|
{
|
66
|
-
key: 'posts/1-
|
66
|
+
key: 'posts/1-dashboard-view'
|
67
67
|
}
|
68
68
|
```
|
69
69
|
|
@@ -75,7 +75,7 @@ Action Controller
|
|
75
75
|
|
76
76
|
```ruby
|
77
77
|
{
|
78
|
-
key: 'posts/1-
|
78
|
+
key: 'posts/1-dashboard-view'
|
79
79
|
}
|
80
80
|
```
|
81
81
|
|
@@ -396,6 +396,15 @@ INFO. Cache stores my add their own keys
|
|
396
396
|
}
|
397
397
|
```
|
398
398
|
|
399
|
+
Railties
|
400
|
+
--------
|
401
|
+
|
402
|
+
### load_config_initializer.railties
|
403
|
+
|
404
|
+
| Key | Value |
|
405
|
+
| -------------- | ----------------------------------------------------- |
|
406
|
+
| `:initializer` | Path to loaded initializer from `config/initializers` |
|
407
|
+
|
399
408
|
Rails
|
400
409
|
-----
|
401
410
|
|
@@ -25,7 +25,7 @@ Write in present tense: "Returns a hash that...", rather than "Returned a hash t
|
|
25
25
|
Start comments in upper case. Follow regular punctuation rules:
|
26
26
|
|
27
27
|
```ruby
|
28
|
-
# Declares an attribute reader backed by an internally-named
|
28
|
+
# Declares an attribute reader backed by an internally-named
|
29
29
|
# instance variable.
|
30
30
|
def attr_internal_reader(*attrs)
|
31
31
|
...
|
@@ -42,7 +42,21 @@ Spell names correctly: Arel, Test::Unit, RSpec, HTML, MySQL, JavaScript, ERB. Wh
|
|
42
42
|
|
43
43
|
Use the article "an" for "SQL", as in "an SQL statement". Also "an SQLite database".
|
44
44
|
|
45
|
-
|
45
|
+
Prefer wordings that avoid "you"s and "your"s. For example, instead of
|
46
|
+
|
47
|
+
```markdown
|
48
|
+
If you need to use `return` statements in your callbacks, it is recommended that you explicitly define them as methods.
|
49
|
+
```
|
50
|
+
|
51
|
+
use this style:
|
52
|
+
|
53
|
+
```markdown
|
54
|
+
If `return` is needed it is recommended to explicitly define a method.
|
55
|
+
```
|
56
|
+
|
57
|
+
That said, when using pronouns in reference to a hypothetical person, such as "a
|
58
|
+
user with a session cookie", gender neutral pronouns (they/their/them) should be
|
59
|
+
used. Instead of:
|
46
60
|
|
47
61
|
* he or she... use they.
|
48
62
|
* him or her... use them.
|
@@ -65,7 +79,7 @@ Use two spaces to indent chunks of code--that is, for markup purposes, two space
|
|
65
79
|
Short docs do not need an explicit "Examples" label to introduce snippets; they just follow paragraphs:
|
66
80
|
|
67
81
|
```ruby
|
68
|
-
# Converts a collection of elements into a formatted string by
|
82
|
+
# Converts a collection of elements into a formatted string by
|
69
83
|
# calling +to_s+ on all elements and joining them.
|
70
84
|
#
|
71
85
|
# Blog.all.to_formatted_s # => "First PostSecond PostThird Post"
|
@@ -149,7 +163,7 @@ class Array
|
|
149
163
|
end
|
150
164
|
```
|
151
165
|
|
152
|
-
WARNING: Using a pair of `+...+` for fixed-width font only works with **words**; that is: anything matching `\A\w+\z`. For anything else
|
166
|
+
WARNING: Using a pair of `+...+` for fixed-width font only works with **words**; that is: anything matching `\A\w+\z`. For anything else use `<tt>...</tt>`, notably symbols, setters, inline snippets, etc.
|
153
167
|
|
154
168
|
### Regular Font
|
155
169
|
|
@@ -180,7 +194,7 @@ In lists of options, parameters, etc. use a hyphen between the item and its desc
|
|
180
194
|
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
|
181
195
|
```
|
182
196
|
|
183
|
-
The description starts in upper case and ends with a full stop
|
197
|
+
The description starts in upper case and ends with a full stop-it's standard English.
|
184
198
|
|
185
199
|
Dynamically Generated Methods
|
186
200
|
-----------------------------
|
@@ -5,9 +5,9 @@ This guide covers the asset pipeline.
|
|
5
5
|
|
6
6
|
After reading this guide, you will know:
|
7
7
|
|
8
|
-
*
|
8
|
+
* What the asset pipeline is and what it does.
|
9
9
|
* How to properly organize your application assets.
|
10
|
-
*
|
10
|
+
* The benefits of the asset pipeline.
|
11
11
|
* How to add a pre-processor to the pipeline.
|
12
12
|
* How to package assets with a gem.
|
13
13
|
|
@@ -16,44 +16,97 @@ After reading this guide, you will know:
|
|
16
16
|
What is the Asset Pipeline?
|
17
17
|
---------------------------
|
18
18
|
|
19
|
-
The asset pipeline provides a framework to concatenate and minify or compress
|
19
|
+
The asset pipeline provides a framework to concatenate and minify or compress
|
20
|
+
JavaScript and CSS assets. It also adds the ability to write these assets in
|
21
|
+
other languages and pre-processors such as CoffeeScript, Sass and ERB.
|
20
22
|
|
21
|
-
|
23
|
+
The asset pipeline is technically no longer a core feature of Rails 4, it has
|
24
|
+
been extracted out of the framework into the
|
25
|
+
[sprockets-rails](https://github.com/rails/sprockets-rails) gem.
|
22
26
|
|
23
|
-
The asset pipeline is enabled by default.
|
27
|
+
The asset pipeline is enabled by default.
|
28
|
+
|
29
|
+
You can disable the asset pipeline while creating a new application by
|
30
|
+
passing the `--skip-sprockets` option.
|
31
|
+
|
32
|
+
```bash
|
33
|
+
rails new appname --skip-sprockets
|
34
|
+
```
|
35
|
+
|
36
|
+
Rails 4 automatically adds the `sass-rails`, `coffee-rails` and `uglifier`
|
37
|
+
gems to your Gemfile, which are used by Sprockets for asset compression:
|
24
38
|
|
25
39
|
```ruby
|
26
|
-
|
40
|
+
gem 'sass-rails'
|
41
|
+
gem 'uglifier'
|
42
|
+
gem 'coffee-rails'
|
27
43
|
```
|
28
44
|
|
29
|
-
|
45
|
+
Using the `--skip-sprockets` option will prevent Rails 4 from adding
|
46
|
+
`sass-rails` and `uglifier` to Gemfile, so if you later want to enable
|
47
|
+
the asset pipeline you will have to add those gems to your Gemfile. Also,
|
48
|
+
creating an application with the `--skip-sprockets` option will generate
|
49
|
+
a slightly different `config/application.rb` file, with a require statement
|
50
|
+
for the sprockets railtie that is commented-out. You will have to remove
|
51
|
+
the comment operator on that line to later enable the asset pipeline:
|
30
52
|
|
31
|
-
```
|
32
|
-
|
53
|
+
```ruby
|
54
|
+
# require "sprockets/railtie"
|
55
|
+
```
|
56
|
+
|
57
|
+
To set asset compression methods, set the appropriate configuration options
|
58
|
+
in `production.rb` - `config.assets.css_compressor` for your CSS and
|
59
|
+
`config.assets.js_compressor` for your Javascript:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
config.assets.css_compressor = :yui
|
63
|
+
config.assets.js_compressor = :uglify
|
33
64
|
```
|
34
65
|
|
35
|
-
|
66
|
+
NOTE: The `sass-rails` gem is automatically used for CSS compression if included
|
67
|
+
in Gemfile and no `config.assets.css_compressor` option is set.
|
36
68
|
|
37
69
|
|
38
70
|
### Main Features
|
39
71
|
|
40
|
-
The first feature of the pipeline is to concatenate assets
|
72
|
+
The first feature of the pipeline is to concatenate assets, which can reduce the
|
73
|
+
number of requests that a browser makes to render a web page. Web browsers are
|
74
|
+
limited in the number of requests that they can make in parallel, so fewer
|
75
|
+
requests can mean faster loading for your application.
|
41
76
|
|
42
|
-
|
77
|
+
Sprockets concatenates all JavaScript files into one master `.js` file and all
|
78
|
+
CSS files into one master `.css` file. As you'll learn later in this guide, you
|
79
|
+
can customize this strategy to group files any way you like. In production,
|
80
|
+
Rails inserts an MD5 fingerprint into each filename so that the file is cached
|
81
|
+
by the web browser. You can invalidate the cache by altering this fingerprint,
|
82
|
+
which happens automatically whenever you change the file contents.
|
43
83
|
|
44
|
-
|
84
|
+
The second feature of the asset pipeline is asset minification or compression.
|
85
|
+
For CSS files, this is done by removing whitespace and comments. For JavaScript,
|
86
|
+
more complex processes can be applied. You can choose from a set of built in
|
87
|
+
options or specify your own.
|
45
88
|
|
46
|
-
The
|
47
|
-
|
48
|
-
|
89
|
+
The third feature of the asset pipeline is it allows coding assets via a
|
90
|
+
higher-level language, with precompilation down to the actual assets. Supported
|
91
|
+
languages include Sass for CSS, CoffeeScript for JavaScript, and ERB for both by
|
92
|
+
default.
|
49
93
|
|
50
94
|
### What is Fingerprinting and Why Should I Care?
|
51
95
|
|
52
|
-
Fingerprinting is a technique that makes the name of a file dependent on the
|
96
|
+
Fingerprinting is a technique that makes the name of a file dependent on the
|
97
|
+
contents of the file. When the file contents change, the filename is also
|
98
|
+
changed. For content that is static or infrequently changed, this provides an
|
99
|
+
easy way to tell whether two versions of a file are identical, even across
|
100
|
+
different servers or deployment dates.
|
53
101
|
|
54
|
-
When a filename is unique and based on its content, HTTP headers can be set to
|
102
|
+
When a filename is unique and based on its content, HTTP headers can be set to
|
103
|
+
encourage caches everywhere (whether at CDNs, at ISPs, in networking equipment,
|
104
|
+
or in web browsers) to keep their own copy of the content. When the content is
|
105
|
+
updated, the fingerprint will change. This will cause the remote clients to
|
106
|
+
request a new copy of the content. This is generally known as _cache busting_.
|
55
107
|
|
56
|
-
The technique
|
108
|
+
The technique sprockets uses for fingerprinting is to insert a hash of the
|
109
|
+
content into the name, usually at the end. For example a CSS file `global.css`
|
57
110
|
|
58
111
|
```
|
59
112
|
global-908e25f4bf641868d8683022a5b62f54.css
|
@@ -61,7 +114,8 @@ global-908e25f4bf641868d8683022a5b62f54.css
|
|
61
114
|
|
62
115
|
This is the strategy adopted by the Rails asset pipeline.
|
63
116
|
|
64
|
-
Rails' old strategy was to append a date-based query string to every asset linked
|
117
|
+
Rails' old strategy was to append a date-based query string to every asset linked
|
118
|
+
with a built-in helper. In the source the generated code looked like this:
|
65
119
|
|
66
120
|
```
|
67
121
|
/stylesheets/global.css?1309495796
|
@@ -69,17 +123,30 @@ Rails' old strategy was to append a date-based query string to every asset linke
|
|
69
123
|
|
70
124
|
The query string strategy has several disadvantages:
|
71
125
|
|
72
|
-
1. **Not all caches will reliably cache content where the filename only differs by
|
73
|
-
|
126
|
+
1. **Not all caches will reliably cache content where the filename only differs by
|
127
|
+
query parameters**<br>
|
128
|
+
[Steve Souders recommends](http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/),
|
129
|
+
"...avoiding a querystring for cacheable resources". He found that in this
|
130
|
+
case 5-20% of requests will not be cached. Query strings in particular do not
|
131
|
+
work at all with some CDNs for cache invalidation.
|
132
|
+
|
133
|
+
2. **The file name can change between nodes in multi-server environments.**<br>
|
134
|
+
The default query string in Rails 2.x is based on the modification time of
|
135
|
+
the files. When assets are deployed to a cluster, there is no guarantee that the
|
136
|
+
timestamps will be the same, resulting in different values being used depending
|
137
|
+
on which server handles the request.
|
74
138
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
139
|
+
3. **Too much cache invalidation**<br>
|
140
|
+
When static assets are deployed with each new release of code, the mtime
|
141
|
+
(time of last modification) of _all_ these files changes, forcing all remote
|
142
|
+
clients to fetch them again, even when the content of those assets has not changed.
|
79
143
|
|
80
|
-
Fingerprinting fixes these problems by avoiding query strings, and by ensuring
|
144
|
+
Fingerprinting fixes these problems by avoiding query strings, and by ensuring
|
145
|
+
that filenames are consistent based on their content.
|
81
146
|
|
82
|
-
Fingerprinting is enabled by default for production and disabled for all other
|
147
|
+
Fingerprinting is enabled by default for production and disabled for all other
|
148
|
+
environments. You can enable or disable it in your configuration through the
|
149
|
+
`config.assets.digest` option.
|
83
150
|
|
84
151
|
More reading:
|
85
152
|
|
@@ -90,41 +157,82 @@ More reading:
|
|
90
157
|
How to Use the Asset Pipeline
|
91
158
|
-----------------------------
|
92
159
|
|
93
|
-
In previous versions of Rails, all assets were located in subdirectories of
|
160
|
+
In previous versions of Rails, all assets were located in subdirectories of
|
161
|
+
`public` such as `images`, `javascripts` and `stylesheets`. With the asset
|
162
|
+
pipeline, the preferred location for these assets is now the `app/assets`
|
163
|
+
directory. Files in this directory are served by the Sprockets middleware.
|
94
164
|
|
95
|
-
Assets can still be placed in the `public` hierarchy. Any assets under `public`
|
165
|
+
Assets can still be placed in the `public` hierarchy. Any assets under `public`
|
166
|
+
will be served as static files by the application or web server. You should use
|
167
|
+
`app/assets` for files that must undergo some pre-processing before they are
|
168
|
+
served.
|
96
169
|
|
97
|
-
In production, Rails precompiles these files to `public/assets` by default. The
|
170
|
+
In production, Rails precompiles these files to `public/assets` by default. The
|
171
|
+
precompiled copies are then served as static assets by the web server. The files
|
172
|
+
in `app/assets` are never served directly in production.
|
98
173
|
|
99
174
|
### Controller Specific Assets
|
100
175
|
|
101
|
-
When you generate a scaffold or a controller, Rails also generates a JavaScript
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
176
|
+
When you generate a scaffold or a controller, Rails also generates a JavaScript
|
177
|
+
file (or CoffeeScript file if the `coffee-rails` gem is in the `Gemfile`) and a
|
178
|
+
Cascading Style Sheet file (or SCSS file if `sass-rails` is in the `Gemfile`)
|
179
|
+
for that controller. Additionally, when generating a scaffold, Rails generates
|
180
|
+
the file scaffolds.css (or scaffolds.css.scss if `sass-rails` is in the
|
181
|
+
`Gemfile`.)
|
182
|
+
|
183
|
+
For example, if you generate a `ProjectsController`, Rails will also add a new
|
184
|
+
file at `app/assets/javascripts/projects.js.coffee` and another at
|
185
|
+
`app/assets/stylesheets/projects.css.scss`. By default these files will be ready
|
186
|
+
to use by your application immediately using the `require_tree` directive. See
|
187
|
+
[Manifest Files and Directives](#manifest-files-and-directives) for more details
|
188
|
+
on require_tree.
|
189
|
+
|
190
|
+
You can also opt to include controller specific stylesheets and JavaScript files
|
191
|
+
only in their respective controllers using the following:
|
192
|
+
|
193
|
+
`<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag
|
194
|
+
params[:controller] %>`
|
195
|
+
|
196
|
+
When doing this, ensure you are not using the `require_tree` directive, as that
|
197
|
+
will result in your assets being included more than once.
|
198
|
+
|
199
|
+
WARNING: When using asset precompilation, you will need to ensure that your
|
200
|
+
controller assets will be precompiled when loading them on a per page basis. By
|
201
|
+
default .coffee and .scss files will not be precompiled on their own. This will
|
202
|
+
result in false positives during development as these files will work just fine
|
203
|
+
since assets are compiled on the fly in development mode. When running in
|
204
|
+
production, however, you will see 500 errors since live compilation is turned
|
205
|
+
off by default. See [Precompiling Assets](#precompiling-assets) for more
|
206
|
+
information on how precompiling works.
|
207
|
+
|
208
|
+
NOTE: You must have an ExecJS supported runtime in order to use CoffeeScript.
|
209
|
+
If you are using Mac OS X or Windows, you have a JavaScript runtime installed in
|
210
|
+
your operating system. Check
|
211
|
+
[ExecJS](https://github.com/sstephenson/execjs#readme) documentation to know all
|
212
|
+
supported JavaScript runtimes.
|
213
|
+
|
214
|
+
You can also disable generation of controller specific asset files by adding the
|
215
|
+
following to your `config/application.rb` configuration:
|
112
216
|
|
113
217
|
```ruby
|
114
|
-
config.generators do |g|
|
115
|
-
|
116
|
-
end
|
218
|
+
config.generators do |g|
|
219
|
+
g.assets false
|
220
|
+
end
|
117
221
|
```
|
118
222
|
|
119
223
|
### Asset Organization
|
120
224
|
|
121
|
-
Pipeline assets can be placed inside an application in one of three locations:
|
225
|
+
Pipeline assets can be placed inside an application in one of three locations:
|
226
|
+
`app/assets`, `lib/assets` or `vendor/assets`.
|
122
227
|
|
123
|
-
* `app/assets` is for assets that are owned by the application, such as custom
|
228
|
+
* `app/assets` is for assets that are owned by the application, such as custom
|
229
|
+
images, JavaScript files or stylesheets.
|
124
230
|
|
125
|
-
* `lib/assets` is for your own libraries' code that doesn't really fit into the
|
231
|
+
* `lib/assets` is for your own libraries' code that doesn't really fit into the
|
232
|
+
scope of the application or those libraries which are shared across applications.
|
126
233
|
|
127
|
-
* `vendor/assets` is for assets that are owned by outside entities, such as
|
234
|
+
* `vendor/assets` is for assets that are owned by outside entities, such as
|
235
|
+
code for JavaScript plugins and CSS frameworks.
|
128
236
|
|
129
237
|
WARNING: If you are upgrading from Rails 3, please take into account that assets
|
130
238
|
under `lib/assets` or `vendor/assets` are available for inclusion via the
|
@@ -133,9 +241,12 @@ application manifests but no longer part of the precompile array. See
|
|
133
241
|
|
134
242
|
#### Search Paths
|
135
243
|
|
136
|
-
When a file is referenced from a manifest or a helper, Sprockets searches the
|
244
|
+
When a file is referenced from a manifest or a helper, Sprockets searches the
|
245
|
+
three default asset locations for it.
|
137
246
|
|
138
|
-
The default locations are: `
|
247
|
+
The default locations are: the `images`, `javascripts` and `stylesheets`
|
248
|
+
directories under the `apps/assets` folder, but these subdirectories
|
249
|
+
are not special - any path under `assets/*` will be searched.
|
139
250
|
|
140
251
|
For example, these files:
|
141
252
|
|
@@ -167,72 +278,113 @@ is referenced as:
|
|
167
278
|
//= require sub/something
|
168
279
|
```
|
169
280
|
|
170
|
-
You can view the search path by inspecting
|
281
|
+
You can view the search path by inspecting
|
282
|
+
`Rails.application.config.assets.paths` in the Rails console.
|
171
283
|
|
172
|
-
Besides the standard `assets/*` paths, additional (fully qualified) paths can be
|
284
|
+
Besides the standard `assets/*` paths, additional (fully qualified) paths can be
|
285
|
+
added to the pipeline in `config/application.rb`. For example:
|
173
286
|
|
174
287
|
```ruby
|
175
288
|
config.assets.paths << Rails.root.join("lib", "videoplayer", "flash")
|
176
289
|
```
|
177
290
|
|
178
|
-
Paths are traversed in the order
|
291
|
+
Paths are traversed in the order they occur in the search path. By default,
|
292
|
+
this means the files in `app/assets` take precedence, and will mask
|
293
|
+
corresponding paths in `lib` and `vendor`.
|
179
294
|
|
180
|
-
It is important to note that files you want to reference outside a manifest must
|
295
|
+
It is important to note that files you want to reference outside a manifest must
|
296
|
+
be added to the precompile array or they will not be available in the production
|
297
|
+
environment.
|
181
298
|
|
182
299
|
#### Using Index Files
|
183
300
|
|
184
|
-
Sprockets uses files named `index` (with the relevant extensions) for a special
|
301
|
+
Sprockets uses files named `index` (with the relevant extensions) for a special
|
302
|
+
purpose.
|
185
303
|
|
186
|
-
For example, if you have a jQuery library with many modules, which is stored in
|
304
|
+
For example, if you have a jQuery library with many modules, which is stored in
|
305
|
+
`lib/assets/library_name`, the file `lib/assets/library_name/index.js` serves as
|
306
|
+
the manifest for all files in this library. This file could include a list of
|
307
|
+
all the required files in order, or a simple `require_tree` directive.
|
187
308
|
|
188
|
-
The library as a whole can be accessed in the
|
309
|
+
The library as a whole can be accessed in the application manifest like so:
|
189
310
|
|
190
311
|
```js
|
191
312
|
//= require library_name
|
192
313
|
```
|
193
314
|
|
194
|
-
This simplifies maintenance and keeps things clean by allowing related code to
|
315
|
+
This simplifies maintenance and keeps things clean by allowing related code to
|
316
|
+
be grouped before inclusion elsewhere.
|
195
317
|
|
196
318
|
### Coding Links to Assets
|
197
319
|
|
198
|
-
Sprockets does not add any new methods to access your assets - you still use the
|
320
|
+
Sprockets does not add any new methods to access your assets - you still use the
|
321
|
+
familiar `javascript_include_tag` and `stylesheet_link_tag`:
|
199
322
|
|
200
323
|
```erb
|
201
|
-
<%= stylesheet_link_tag "application" %>
|
324
|
+
<%= stylesheet_link_tag "application", media: "all" %>
|
202
325
|
<%= javascript_include_tag "application" %>
|
203
326
|
```
|
204
327
|
|
205
|
-
|
328
|
+
If using the turbolinks gem, which is included by default in Rails 4, then
|
329
|
+
include the 'data-turbolinks-track' option which causes turbolinks to check if
|
330
|
+
an asset has been updated and if so loads it into the page:
|
331
|
+
|
332
|
+
```erb
|
333
|
+
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
|
334
|
+
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
|
335
|
+
```
|
336
|
+
|
337
|
+
In regular views you can access images in the `public/assets/images` directory
|
338
|
+
like this:
|
206
339
|
|
207
340
|
```erb
|
208
341
|
<%= image_tag "rails.png" %>
|
209
342
|
```
|
210
343
|
|
211
|
-
Provided that the pipeline is enabled within your application (and not disabled
|
344
|
+
Provided that the pipeline is enabled within your application (and not disabled
|
345
|
+
in the current environment context), this file is served by Sprockets. If a file
|
346
|
+
exists at `public/assets/rails.png` it is served by the web server.
|
212
347
|
|
213
|
-
Alternatively, a request for a file with an MD5 hash such as
|
348
|
+
Alternatively, a request for a file with an MD5 hash such as
|
349
|
+
`public/assets/rails-af27b6a414e6da00003503148be9b409.png` is treated the same
|
350
|
+
way. How these hashes are generated is covered in the [In
|
351
|
+
Production](#in-production) section later on in this guide.
|
214
352
|
|
215
|
-
Sprockets will also look through the paths specified in `config.assets.paths
|
353
|
+
Sprockets will also look through the paths specified in `config.assets.paths`,
|
354
|
+
which includes the standard application paths and any paths added by Rails
|
355
|
+
engines.
|
216
356
|
|
217
|
-
Images can also be organized into subdirectories if required, and
|
357
|
+
Images can also be organized into subdirectories if required, and then can be
|
358
|
+
accessed by specifying the directory's name in the tag:
|
218
359
|
|
219
360
|
```erb
|
220
361
|
<%= image_tag "icons/rails.png" %>
|
221
362
|
```
|
222
363
|
|
223
|
-
WARNING: If you're precompiling your assets (see [In Production](#in-production)
|
364
|
+
WARNING: If you're precompiling your assets (see [In Production](#in-production)
|
365
|
+
below), linking to an asset that does not exist will raise an exception in the
|
366
|
+
calling page. This includes linking to a blank string. As such, be careful using
|
367
|
+
`image_tag` and the other helpers with user-supplied data.
|
224
368
|
|
225
369
|
#### CSS and ERB
|
226
370
|
|
227
|
-
The asset pipeline automatically evaluates ERB. This means
|
371
|
+
The asset pipeline automatically evaluates ERB. This means if you add an
|
372
|
+
`erb` extension to a CSS asset (for example, `application.css.erb`), then
|
373
|
+
helpers like `asset_path` are available in your CSS rules:
|
228
374
|
|
229
375
|
```css
|
230
376
|
.class { background-image: url(<%= asset_path 'image.png' %>) }
|
231
377
|
```
|
232
378
|
|
233
|
-
This writes the path to the particular asset being referenced. In this example,
|
379
|
+
This writes the path to the particular asset being referenced. In this example,
|
380
|
+
it would make sense to have an image in one of the asset load paths, such as
|
381
|
+
`app/assets/images/image.png`, which would be referenced here. If this image is
|
382
|
+
already available in `public/assets` as a fingerprinted file, then that path is
|
383
|
+
referenced.
|
234
384
|
|
235
|
-
If you want to use a [data URI](http://en.wikipedia.org/wiki/Data_URI_scheme)
|
385
|
+
If you want to use a [data URI](http://en.wikipedia.org/wiki/Data_URI_scheme) -
|
386
|
+
a method of embedding the image data directly into the CSS file - you can use
|
387
|
+
the `asset_data_uri` helper.
|
236
388
|
|
237
389
|
```css
|
238
390
|
#logo { background: url(<%= asset_data_uri 'logo.png' %>) }
|
@@ -244,7 +396,10 @@ Note that the closing tag cannot be of the style `-%>`.
|
|
244
396
|
|
245
397
|
#### CSS and Sass
|
246
398
|
|
247
|
-
When using the asset pipeline, paths to assets must be re-written and
|
399
|
+
When using the asset pipeline, paths to assets must be re-written and
|
400
|
+
`sass-rails` provides `-url` and `-path` helpers (hyphenated in Sass,
|
401
|
+
underscored in Ruby) for the following asset classes: image, font, video, audio,
|
402
|
+
JavaScript and stylesheet.
|
248
403
|
|
249
404
|
* `image-url("rails.png")` becomes `url(/assets/rails.png)`
|
250
405
|
* `image-path("rails.png")` becomes `"/assets/rails.png"`.
|
@@ -256,17 +411,18 @@ The more generic form can also be used:
|
|
256
411
|
|
257
412
|
#### JavaScript/CoffeeScript and ERB
|
258
413
|
|
259
|
-
If you add an `erb` extension to a JavaScript asset, making it something such as
|
414
|
+
If you add an `erb` extension to a JavaScript asset, making it something such as
|
415
|
+
`application.js.erb`, you can then use the `asset_path` helper in your
|
416
|
+
JavaScript code:
|
260
417
|
|
261
418
|
```js
|
262
|
-
$('#logo').attr({
|
263
|
-
src: "<%= asset_path('logo.png') %>"
|
264
|
-
});
|
419
|
+
$('#logo').attr({ src: "<%= asset_path('logo.png') %>" });
|
265
420
|
```
|
266
421
|
|
267
422
|
This writes the path to the particular asset being referenced.
|
268
423
|
|
269
|
-
Similarly, you can use the `asset_path` helper in CoffeeScript files with `erb`
|
424
|
+
Similarly, you can use the `asset_path` helper in CoffeeScript files with `erb`
|
425
|
+
extension (e.g., `application.js.coffee.erb`):
|
270
426
|
|
271
427
|
```js
|
272
428
|
$('#logo').attr src: "<%= asset_path('logo.png') %>"
|
@@ -274,10 +430,19 @@ $('#logo').attr src: "<%= asset_path('logo.png') %>"
|
|
274
430
|
|
275
431
|
### Manifest Files and Directives
|
276
432
|
|
277
|
-
Sprockets uses manifest files to determine which assets to include and serve.
|
433
|
+
Sprockets uses manifest files to determine which assets to include and serve.
|
434
|
+
These manifest files contain _directives_ - instructions that tell Sprockets
|
435
|
+
which files to require in order to build a single CSS or JavaScript file. With
|
436
|
+
these directives, Sprockets loads the files specified, processes them if
|
437
|
+
necessary, concatenates them into one single file and then compresses them (if
|
438
|
+
`Rails.application.config.assets.compress` is true). By serving one file rather
|
439
|
+
than many, the load time of pages can be greatly reduced because the browser
|
440
|
+
makes fewer requests. Compression also reduces file size, enabling the
|
441
|
+
browser to download them faster.
|
278
442
|
|
279
443
|
|
280
|
-
For example, a new Rails application includes a default
|
444
|
+
For example, a new Rails 4 application includes a default
|
445
|
+
`app/assets/javascripts/application.js` file containing the following lines:
|
281
446
|
|
282
447
|
```js
|
283
448
|
// ...
|
@@ -286,30 +451,69 @@ For example, a new Rails application includes a default `app/assets/javascripts/
|
|
286
451
|
//= require_tree .
|
287
452
|
```
|
288
453
|
|
289
|
-
In JavaScript files,
|
454
|
+
In JavaScript files, Sprockets directives begin with `//=`. In the above case,
|
455
|
+
the file is using the `require` and the `require_tree` directives. The `require`
|
456
|
+
directive is used to tell Sprockets the files you wish to require. Here, you are
|
457
|
+
requiring the files `jquery.js` and `jquery_ujs.js` that are available somewhere
|
458
|
+
in the search path for Sprockets. You need not supply the extensions explicitly.
|
459
|
+
Sprockets assumes you are requiring a `.js` file when done from within a `.js`
|
460
|
+
file.
|
461
|
+
|
462
|
+
The `require_tree` directive tells Sprockets to recursively include _all_
|
463
|
+
JavaScript files in the specified directory into the output. These paths must be
|
464
|
+
specified relative to the manifest file. You can also use the
|
465
|
+
`require_directory` directive which includes all JavaScript files only in the
|
466
|
+
directory specified, without recursion.
|
467
|
+
|
468
|
+
Directives are processed top to bottom, but the order in which files are
|
469
|
+
included by `require_tree` is unspecified. You should not rely on any particular
|
470
|
+
order among those. If you need to ensure some particular JavaScript ends up
|
471
|
+
above some other in the concatenated file, require the prerequisite file first
|
472
|
+
in the manifest. Note that the family of `require` directives prevents files
|
473
|
+
from being included twice in the output.
|
474
|
+
|
475
|
+
Rails also creates a default `app/assets/stylesheets/application.css` file
|
476
|
+
which contains these lines:
|
290
477
|
|
291
|
-
|
292
|
-
|
293
|
-
Directives are processed top to bottom, but the order in which files are included by `require_tree` is unspecified. You should not rely on any particular order among those. If you need to ensure some particular JavaScript ends up above some other in the concatenated file, require the prerequisite file first in the manifest. Note that the family of `require` directives prevents files from being included twice in the output.
|
294
|
-
|
295
|
-
Rails also creates a default `app/assets/stylesheets/application.css` file which contains these lines:
|
296
|
-
|
297
|
-
```js
|
478
|
+
```css
|
298
479
|
/* ...
|
299
480
|
*= require_self
|
300
481
|
*= require_tree .
|
301
482
|
*/
|
302
483
|
```
|
303
484
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
485
|
+
Rails 4 creates both `app/assets/javascripts/application.js` and
|
486
|
+
`app/assets/stylesheets/application.css` regardless of whether the
|
487
|
+
--skip-sprockets option is used when creating a new rails application. This is
|
488
|
+
so you can easily add asset pipelining later if you like.
|
489
|
+
|
490
|
+
The directives that work in JavaScript files also work in stylesheets
|
491
|
+
(though obviously including stylesheets rather than JavaScript files). The
|
492
|
+
`require_tree` directive in a CSS manifest works the same way as the JavaScript
|
493
|
+
one, requiring all stylesheets from the current directory.
|
494
|
+
|
495
|
+
In this example, `require_self` is used. This puts the CSS contained within the
|
496
|
+
file (if any) at the precise location of the `require_self` call. If
|
497
|
+
`require_self` is called more than once, only the last call is respected.
|
498
|
+
|
499
|
+
NOTE. If you want to use multiple Sass files, you should generally use the [Sass
|
500
|
+
`@import`
|
501
|
+
rule](http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#import) instead
|
502
|
+
of these Sprockets directives. Using Sprockets directives all Sass files exist
|
503
|
+
within their own scope, making variables or mixins only available within the
|
504
|
+
document they were defined in. You can do file globbing as well using
|
505
|
+
`@import "*"`, and `@import "**/*"` to add the whole tree equivalent to how
|
506
|
+
`require_tree` works. Check the [sass-rails
|
507
|
+
documentation](https://github.com/rails/sass-rails#features) for more info and
|
508
|
+
important caveats.
|
509
|
+
|
510
|
+
You can have as many manifest files as you need. For example, the `admin.css`
|
511
|
+
and `admin.js` manifest could contain the JS and CSS files that are used for the
|
512
|
+
admin section of an application.
|
513
|
+
|
514
|
+
The same remarks about ordering made above apply. In particular, you can specify
|
515
|
+
individual files and they are compiled in the order specified. For example, you
|
516
|
+
might concatenate three CSS files together this way:
|
313
517
|
|
314
518
|
```js
|
315
519
|
/* ...
|
@@ -319,21 +523,41 @@ The same remarks about ordering made above apply. In particular, you can specify
|
|
319
523
|
*/
|
320
524
|
```
|
321
525
|
|
322
|
-
|
323
526
|
### Preprocessing
|
324
527
|
|
325
|
-
The file extensions used on an asset determine what preprocessing is applied.
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
528
|
+
The file extensions used on an asset determine what preprocessing is applied.
|
529
|
+
When a controller or a scaffold is generated with the default Rails gemset, a
|
530
|
+
CoffeeScript file and a SCSS file are generated in place of a regular JavaScript
|
531
|
+
and CSS file. The example used before was a controller called "projects", which
|
532
|
+
generated an `app/assets/javascripts/projects.js.coffee` and an
|
533
|
+
`app/assets/stylesheets/projects.css.scss` file.
|
534
|
+
|
535
|
+
In development mode, or if the asset pipeline is disabled, when these files are
|
536
|
+
requested they are processed by the processors provided by the `coffee-script`
|
537
|
+
and `sass` gems and then sent back to the browser as JavaScript and CSS
|
538
|
+
respectively. When asset pipelining is enabled, these files are preprocessed and
|
539
|
+
placed in the `public/assets` directory for serving by either the Rails app or
|
540
|
+
web server.
|
541
|
+
|
542
|
+
Additional layers of preprocessing can be requested by adding other extensions,
|
543
|
+
where each extension is processed in a right-to-left manner. These should be
|
544
|
+
used in the order the processing should be applied. For example, a stylesheet
|
545
|
+
called `app/assets/stylesheets/projects.css.scss.erb` is first processed as ERB,
|
546
|
+
then SCSS, and finally served as CSS. The same applies to a JavaScript file -
|
547
|
+
`app/assets/javascripts/projects.js.coffee.erb` is processed as ERB, then
|
548
|
+
CoffeeScript, and served as JavaScript.
|
549
|
+
|
550
|
+
Keep in mind the order of these preprocessors is important. For example, if
|
551
|
+
you called your JavaScript file `app/assets/javascripts/projects.js.erb.coffee`
|
552
|
+
then it would be processed with the CoffeeScript interpreter first, which
|
553
|
+
wouldn't understand ERB and therefore you would run into problems.
|
330
554
|
|
331
|
-
Keep in mind that the order of these preprocessors is important. For example, if you called your JavaScript file `app/assets/javascripts/projects.js.erb.coffee` then it would be processed with the CoffeeScript interpreter first, which wouldn't understand ERB and therefore you would run into problems.
|
332
555
|
|
333
556
|
In Development
|
334
557
|
--------------
|
335
558
|
|
336
|
-
In development mode, assets are served as separate files in the order they are
|
559
|
+
In development mode, assets are served as separate files in the order they are
|
560
|
+
specified in the manifest file.
|
337
561
|
|
338
562
|
This manifest `app/assets/javascripts/application.js`:
|
339
563
|
|
@@ -355,39 +579,52 @@ The `body` param is required by Sprockets.
|
|
355
579
|
|
356
580
|
### Turning Debugging Off
|
357
581
|
|
358
|
-
You can turn off debug mode by updating `config/environments/development.rb` to
|
582
|
+
You can turn off debug mode by updating `config/environments/development.rb` to
|
583
|
+
include:
|
359
584
|
|
360
585
|
```ruby
|
361
586
|
config.assets.debug = false
|
362
587
|
```
|
363
588
|
|
364
|
-
When debug mode is off, Sprockets concatenates and runs the necessary
|
589
|
+
When debug mode is off, Sprockets concatenates and runs the necessary
|
590
|
+
preprocessors on all files. With debug mode turned off the manifest above would
|
591
|
+
generate instead:
|
365
592
|
|
366
593
|
```html
|
367
594
|
<script src="/assets/application.js"></script>
|
368
595
|
```
|
369
596
|
|
370
|
-
Assets are compiled and cached on the first request after the server is started.
|
597
|
+
Assets are compiled and cached on the first request after the server is started.
|
598
|
+
Sprockets sets a `must-revalidate` Cache-Control HTTP header to reduce request
|
599
|
+
overhead on subsequent requests - on these the browser gets a 304 (Not Modified)
|
600
|
+
response.
|
371
601
|
|
372
|
-
If any of the files in the manifest have changed between requests, the server
|
602
|
+
If any of the files in the manifest have changed between requests, the server
|
603
|
+
responds with a new compiled file.
|
373
604
|
|
374
|
-
Debug mode can also be enabled in
|
605
|
+
Debug mode can also be enabled in Rails helper methods:
|
375
606
|
|
376
607
|
```erb
|
377
608
|
<%= stylesheet_link_tag "application", debug: true %>
|
378
609
|
<%= javascript_include_tag "application", debug: true %>
|
379
610
|
```
|
380
611
|
|
381
|
-
The `:debug` option is redundant if debug mode is on.
|
612
|
+
The `:debug` option is redundant if debug mode is already on.
|
382
613
|
|
383
|
-
You
|
614
|
+
You can also enable compression in development mode as a sanity check, and
|
615
|
+
disable it on-demand as required for debugging.
|
384
616
|
|
385
617
|
In Production
|
386
618
|
-------------
|
387
619
|
|
388
|
-
In the production environment
|
620
|
+
In the production environment Sprockets uses the fingerprinting scheme outlined
|
621
|
+
above. By default Rails assumes assets have been precompiled and will be
|
622
|
+
served as static assets by your web server.
|
389
623
|
|
390
|
-
During the precompilation phase an MD5 is generated from the contents of the
|
624
|
+
During the precompilation phase an MD5 is generated from the contents of the
|
625
|
+
compiled files, and inserted into the filenames as they are written to disc.
|
626
|
+
These fingerprinted names are used by the Rails helpers in place of the manifest
|
627
|
+
name.
|
391
628
|
|
392
629
|
For example this:
|
393
630
|
|
@@ -400,23 +637,34 @@ generates something like this:
|
|
400
637
|
|
401
638
|
```html
|
402
639
|
<script src="/assets/application-908e25f4bf641868d8683022a5b62f54.js"></script>
|
403
|
-
<link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen"
|
640
|
+
<link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen"
|
641
|
+
rel="stylesheet" />
|
404
642
|
```
|
405
643
|
|
406
|
-
Note: with the Asset Pipeline the :cache and :concat options aren't used
|
644
|
+
Note: with the Asset Pipeline the :cache and :concat options aren't used
|
645
|
+
anymore, delete these options from the `javascript_include_tag` and
|
646
|
+
`stylesheet_link_tag`.
|
407
647
|
|
648
|
+
The fingerprinting behavior is controlled by the `config.assets.digest`
|
649
|
+
initialization option (which defaults to `true` for production and `false` for
|
650
|
+
everything else).
|
408
651
|
|
409
|
-
|
410
|
-
|
411
|
-
|
652
|
+
NOTE: Under normal circumstances the default `config.assets.digest` option
|
653
|
+
should not be changed. If there are no digests in the filenames, and far-future
|
654
|
+
headers are set, remote clients will never know to refetch the files when their
|
655
|
+
content changes.
|
412
656
|
|
413
657
|
### Precompiling Assets
|
414
658
|
|
415
|
-
Rails comes bundled with a rake task to compile the asset manifests and other
|
659
|
+
Rails comes bundled with a rake task to compile the asset manifests and other
|
660
|
+
files in the pipeline.
|
416
661
|
|
417
|
-
Compiled assets are written to the location specified in `config.assets.prefix`.
|
662
|
+
Compiled assets are written to the location specified in `config.assets.prefix`.
|
663
|
+
By default, this is the `/assets` directory.
|
418
664
|
|
419
|
-
You can call this task on the server during deployment to create compiled
|
665
|
+
You can call this task on the server during deployment to create compiled
|
666
|
+
versions of your assets directly on the server. See the next section for
|
667
|
+
information on compiling locally.
|
420
668
|
|
421
669
|
The rake task is:
|
422
670
|
|
@@ -424,26 +672,20 @@ The rake task is:
|
|
424
672
|
$ RAILS_ENV=production bundle exec rake assets:precompile
|
425
673
|
```
|
426
674
|
|
427
|
-
|
428
|
-
|
429
|
-
cannot see application objects or methods. **Heroku requires this to be false.**
|
430
|
-
|
431
|
-
WARNING: If you set `config.assets.initialize_on_precompile` to false, be sure to
|
432
|
-
test `rake assets:precompile` locally before deploying. It may expose bugs where
|
433
|
-
your assets reference application objects or methods, since those are still
|
434
|
-
in scope in development mode regardless of the value of this flag. Changing this flag also affects
|
435
|
-
engines. Engines can define assets for precompilation as well. Since the complete environment is not loaded,
|
436
|
-
engines (or other gems) will not be loaded, which can cause missing assets.
|
437
|
-
|
438
|
-
Capistrano (v2.8.0 and above) includes a recipe to handle this in deployment. Add the following line to `Capfile`:
|
675
|
+
Capistrano (v2.15.1 and above) includes a recipe to handle this in deployment.
|
676
|
+
Add the following line to `Capfile`:
|
439
677
|
|
440
678
|
```ruby
|
441
679
|
load 'deploy/assets'
|
442
680
|
```
|
443
681
|
|
444
|
-
This links the folder specified in `config.assets.prefix` to `shared/assets`.
|
682
|
+
This links the folder specified in `config.assets.prefix` to `shared/assets`.
|
683
|
+
If you already use this shared folder you'll need to write your own deployment
|
684
|
+
task.
|
445
685
|
|
446
|
-
It is important that this folder is shared between deployments so that remotely
|
686
|
+
It is important that this folder is shared between deployments so that remotely
|
687
|
+
cached pages referencing the old compiled assets still work for the life of
|
688
|
+
the cached page.
|
447
689
|
|
448
690
|
The default matcher for compiling files includes `application.js`,
|
449
691
|
`application.css` and all non-JS/CSS files (this will include all image assets
|
@@ -454,15 +696,19 @@ automatically) from `app/assets` folders including your gems:
|
|
454
696
|
/application.(css|js)$/ ]
|
455
697
|
```
|
456
698
|
|
457
|
-
NOTE
|
699
|
+
NOTE: The matcher (and other members of the precompile array; see below) is
|
700
|
+
applied to final compiled file names. This means anything that compiles to
|
701
|
+
JS/CSS is excluded, as well as raw JS/CSS files; for example, `.coffee` and
|
702
|
+
`.scss` files are **not** automatically included as they compile to JS/CSS.
|
458
703
|
|
459
|
-
If you have other manifests or individual stylesheets and JavaScript files to
|
704
|
+
If you have other manifests or individual stylesheets and JavaScript files to
|
705
|
+
include, you can add them to the `precompile` array in `config/application.rb`:
|
460
706
|
|
461
707
|
```ruby
|
462
708
|
config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
|
463
709
|
```
|
464
710
|
|
465
|
-
Or you can opt to precompile all assets with something like this:
|
711
|
+
Or, you can opt to precompile all assets with something like this:
|
466
712
|
|
467
713
|
```ruby
|
468
714
|
# config/application.rb
|
@@ -483,38 +729,51 @@ config.assets.precompile << Proc.new do |path|
|
|
483
729
|
end
|
484
730
|
```
|
485
731
|
|
486
|
-
NOTE. Always specify an expected compiled filename that ends with js or css,
|
732
|
+
NOTE. Always specify an expected compiled filename that ends with .js or .css,
|
733
|
+
even if you want to add Sass or CoffeeScript files to the precompile array.
|
487
734
|
|
488
|
-
The rake task also generates a `manifest.
|
735
|
+
The rake task also generates a `manifest-md5hash.json` that contains a list with
|
736
|
+
all your assets and their respective fingerprints. This is used by the Rails
|
737
|
+
helper methods to avoid handing the mapping requests back to Sprockets. A
|
738
|
+
typical manifest file looks like:
|
489
739
|
|
490
|
-
```
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
application.
|
740
|
+
```ruby
|
741
|
+
{"files":{"application-723d1be6cc741a3aabb1cec24276d681.js":{"logical_path":"application.js","mtime":"2013-07-26T22:55:03-07:00","size":302506,
|
742
|
+
"digest":"723d1be6cc741a3aabb1cec24276d681"},"application-12b3c7dd74d2e9df37e7cbb1efa76a6d.css":{"logical_path":"application.css","mtime":"2013-07-26T22:54:54-07:00","size":1560,
|
743
|
+
"digest":"12b3c7dd74d2e9df37e7cbb1efa76a6d"},"application-1c5752789588ac18d7e1a50b1f0fd4c2.css":{"logical_path":"application.css","mtime":"2013-07-26T22:56:17-07:00","size":1591,
|
744
|
+
"digest":"1c5752789588ac18d7e1a50b1f0fd4c2"},"favicon-a9c641bf2b81f0476e876f7c5e375969.ico":{"logical_path":"favicon.ico","mtime":"2013-07-26T23:00:10-07:00","size":1406,
|
745
|
+
"digest":"a9c641bf2b81f0476e876f7c5e375969"},"my_image-231a680f23887d9dd70710ea5efd3c62.png":{"logical_path":"my_image.png","mtime":"2013-07-26T23:00:27-07:00","size":6646,
|
746
|
+
"digest":"231a680f23887d9dd70710ea5efd3c62"}},"assets"{"application.js":
|
747
|
+
"application-723d1be6cc741a3aabb1cec24276d681.js","application.css":
|
748
|
+
"application-1c5752789588ac18d7e1a50b1f0fd4c2.css",
|
749
|
+
"favicon.ico":"favicona9c641bf2b81f0476e876f7c5e375969.ico","my_image.png":
|
750
|
+
"my_image-231a680f23887d9dd70710ea5efd3c62.png"}}
|
497
751
|
```
|
498
752
|
|
499
|
-
The default location for the manifest is the root of the location specified in
|
753
|
+
The default location for the manifest is the root of the location specified in
|
754
|
+
`config.assets.prefix` ('/assets' by default).
|
500
755
|
|
501
|
-
NOTE: If there are missing precompiled files in production you will get an
|
756
|
+
NOTE: If there are missing precompiled files in production you will get an
|
757
|
+
`Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError`
|
758
|
+
exception indicating the name of the missing file(s).
|
502
759
|
|
503
760
|
#### Far-future Expires Header
|
504
761
|
|
505
|
-
Precompiled assets exist on the filesystem and are served directly by your web
|
762
|
+
Precompiled assets exist on the filesystem and are served directly by your web
|
763
|
+
server. They do not have far-future headers by default, so to get the benefit of
|
764
|
+
fingerprinting you'll have to update your server configuration to add those
|
765
|
+
headers.
|
506
766
|
|
507
767
|
For Apache:
|
508
768
|
|
509
769
|
```apache
|
510
|
-
# The Expires* directives requires the Apache module
|
770
|
+
# The Expires* directives requires the Apache module
|
771
|
+
# `mod_expires` to be enabled.
|
511
772
|
<Location /assets/>
|
512
773
|
# Use of ETag is discouraged when Last-Modified is present
|
513
|
-
Header unset ETag
|
514
|
-
FileETag None
|
774
|
+
Header unset ETag FileETag None
|
515
775
|
# RFC says only cache for 1 year
|
516
|
-
ExpiresActive On
|
517
|
-
ExpiresDefault "access plus 1 year"
|
776
|
+
ExpiresActive On ExpiresDefault "access plus 1 year"
|
518
777
|
</Location>
|
519
778
|
```
|
520
779
|
|
@@ -532,7 +791,13 @@ location ~ ^/assets/ {
|
|
532
791
|
|
533
792
|
#### GZip Compression
|
534
793
|
|
535
|
-
When files are precompiled, Sprockets also creates a
|
794
|
+
When files are precompiled, Sprockets also creates a
|
795
|
+
[gzipped](http://en.wikipedia.org/wiki/Gzip) (.gz) version of your assets. Web
|
796
|
+
servers are typically configured to use a moderate compression ratio as a
|
797
|
+
compromise, but since precompilation happens once, Sprockets uses the maximum
|
798
|
+
compression ratio, thus reducing the size of the data transfer to the minimum.
|
799
|
+
On the other hand, web servers can be configured to serve compressed content
|
800
|
+
directly from disk, rather than deflating non-compressed files themselves.
|
536
801
|
|
537
802
|
Nginx is able to do this automatically enabling `gzip_static`:
|
538
803
|
|
@@ -545,25 +810,32 @@ location ~ ^/(assets)/ {
|
|
545
810
|
}
|
546
811
|
```
|
547
812
|
|
548
|
-
This directive is available if the core module that provides this feature was
|
813
|
+
This directive is available if the core module that provides this feature was
|
814
|
+
compiled with the web server. Ubuntu/Debian packages, even `nginx-light`, have
|
815
|
+
the module compiled. Otherwise, you may need to perform a manual compilation:
|
549
816
|
|
550
817
|
```bash
|
551
818
|
./configure --with-http_gzip_static_module
|
552
819
|
```
|
553
820
|
|
554
|
-
If you're compiling nginx with Phusion Passenger you'll need to pass that option
|
821
|
+
If you're compiling nginx with Phusion Passenger you'll need to pass that option
|
822
|
+
when prompted.
|
555
823
|
|
556
|
-
A robust configuration for Apache is possible but tricky; please Google around.
|
824
|
+
A robust configuration for Apache is possible but tricky; please Google around.
|
825
|
+
(Or help update this Guide if you have a good configuration example for Apache.)
|
557
826
|
|
558
827
|
### Local Precompilation
|
559
828
|
|
560
|
-
There are several reasons why you might want to precompile your assets locally.
|
829
|
+
There are several reasons why you might want to precompile your assets locally.
|
830
|
+
Among them are:
|
561
831
|
|
562
832
|
* You may not have write access to your production file system.
|
563
|
-
* You may be deploying to more than one server, and want to avoid
|
833
|
+
* You may be deploying to more than one server, and want to avoid
|
834
|
+
duplication of work.
|
564
835
|
* You may be doing frequent deploys that do not include asset changes.
|
565
836
|
|
566
|
-
Local compilation allows you to commit the compiled files into source control,
|
837
|
+
Local compilation allows you to commit the compiled files into source control,
|
838
|
+
and deploy as normal.
|
567
839
|
|
568
840
|
There are two caveats:
|
569
841
|
|
@@ -576,23 +848,23 @@ In `config/environments/development.rb`, place the following line:
|
|
576
848
|
config.assets.prefix = "/dev-assets"
|
577
849
|
```
|
578
850
|
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
The `prefix` change makes Rails use a different URL for serving assets in development mode, and pass all requests to Sprockets. The prefix is still set to `/assets` in the production environment. Without this change, the application would serve the precompiled assets from `public/assets` in development, and you would not see any local changes until you compile assets again.
|
851
|
+
The `prefix` change makes Sprockets use a different URL for serving assets in
|
852
|
+
development mode, and pass all requests to Sprockets. The prefix is still set to
|
853
|
+
`/assets` in the production environment. Without this change, the application
|
854
|
+
would serve the precompiled assets from `/assets` in development, and you would
|
855
|
+
not see any local changes until you compile assets again.
|
586
856
|
|
587
|
-
|
857
|
+
You will also need to ensure any necessary compressors or minifiers are
|
858
|
+
available on your development system.
|
588
859
|
|
589
|
-
|
590
|
-
|
591
|
-
|
860
|
+
In practice, this will allow you to precompile locally, have those files in your
|
861
|
+
working tree, and commit those files to source control when needed. Development
|
862
|
+
mode will work as expected.
|
592
863
|
|
593
864
|
### Live Compilation
|
594
865
|
|
595
|
-
In some circumstances you may wish to use live compilation. In this mode all
|
866
|
+
In some circumstances you may wish to use live compilation. In this mode all
|
867
|
+
requests for assets in the pipeline are handled by Sprockets directly.
|
596
868
|
|
597
869
|
To enable this option set:
|
598
870
|
|
@@ -600,13 +872,21 @@ To enable this option set:
|
|
600
872
|
config.assets.compile = true
|
601
873
|
```
|
602
874
|
|
603
|
-
On the first request the assets are compiled and cached as outlined in
|
875
|
+
On the first request the assets are compiled and cached as outlined in
|
876
|
+
development above, and the manifest names used in the helpers are altered to
|
877
|
+
include the MD5 hash.
|
604
878
|
|
605
|
-
Sprockets also sets the `Cache-Control` HTTP header to `max-age=31536000`. This
|
879
|
+
Sprockets also sets the `Cache-Control` HTTP header to `max-age=31536000`. This
|
880
|
+
signals all caches between your server and the client browser that this content
|
881
|
+
(the file served) can be cached for 1 year. The effect of this is to reduce the
|
882
|
+
number of requests for this asset from your server; the asset has a good chance
|
883
|
+
of being in the local browser cache or some intermediate cache.
|
606
884
|
|
607
|
-
This mode uses more memory, performs more poorly than the default and is not
|
885
|
+
This mode uses more memory, performs more poorly than the default and is not
|
886
|
+
recommended.
|
608
887
|
|
609
|
-
If you are deploying a production application to a system without any
|
888
|
+
If you are deploying a production application to a system without any
|
889
|
+
pre-existing JavaScript runtimes, you may want to add one to your Gemfile:
|
610
890
|
|
611
891
|
```ruby
|
612
892
|
group :production do
|
@@ -616,36 +896,43 @@ end
|
|
616
896
|
|
617
897
|
### CDNs
|
618
898
|
|
619
|
-
If your assets are being served by a CDN, ensure they don't stick around in
|
620
|
-
|
899
|
+
If your assets are being served by a CDN, ensure they don't stick around in your
|
900
|
+
cache forever. This can cause problems. If you use
|
621
901
|
`config.action_controller.perform_caching = true`, Rack::Cache will use
|
622
902
|
`Rails.cache` to store assets. This can cause your cache to fill up quickly.
|
623
903
|
|
624
|
-
Every cache is different, so evaluate how your CDN handles caching and make
|
625
|
-
|
626
|
-
|
627
|
-
|
904
|
+
Every cache is different, so evaluate how your CDN handles caching and make sure
|
905
|
+
that it plays nicely with the pipeline. You may find quirks related to your
|
906
|
+
specific set up, you may not. The defaults nginx uses, for example, should give
|
907
|
+
you no problems when used as an HTTP cache.
|
628
908
|
|
629
909
|
Customizing the Pipeline
|
630
910
|
------------------------
|
631
911
|
|
632
912
|
### CSS Compression
|
633
913
|
|
634
|
-
There is currently one option for compressing CSS, YUI. The [YUI CSS
|
914
|
+
There is currently one option for compressing CSS, YUI. The [YUI CSS
|
915
|
+
compressor](http://yui.github.io/yuicompressor/css.html) provides
|
916
|
+
minification.
|
635
917
|
|
636
|
-
The following line enables YUI compression, and requires the `yui-compressor`
|
918
|
+
The following line enables YUI compression, and requires the `yui-compressor`
|
919
|
+
gem.
|
637
920
|
|
638
921
|
```ruby
|
639
922
|
config.assets.css_compressor = :yui
|
640
923
|
```
|
641
924
|
|
642
|
-
The `config.assets.compress` must be set to `true` to enable CSS compression.
|
643
|
-
|
644
925
|
### JavaScript Compression
|
645
926
|
|
646
|
-
Possible options for JavaScript compression are `:closure`, `:uglifier` and
|
927
|
+
Possible options for JavaScript compression are `:closure`, `:uglifier` and
|
928
|
+
`:yui`. These require the use of the `closure-compiler`, `uglifier` or
|
929
|
+
`yui-compressor` gems, respectively.
|
647
930
|
|
648
|
-
The default Gemfile includes [uglifier](https://github.com/lautis/uglifier).
|
931
|
+
The default Gemfile includes [uglifier](https://github.com/lautis/uglifier).
|
932
|
+
This gem wraps [UglifyJS](https://github.com/mishoo/UglifyJS) (written for
|
933
|
+
NodeJS) in Ruby. It compresses your code by removing white space and comments,
|
934
|
+
shortening local variable names, and performing other micro-optimizations such
|
935
|
+
as changing `if` and `else` statements to ternary operators where possible.
|
649
936
|
|
650
937
|
The following line invokes `uglifier` for JavaScript compression.
|
651
938
|
|
@@ -653,13 +940,21 @@ The following line invokes `uglifier` for JavaScript compression.
|
|
653
940
|
config.assets.js_compressor = :uglifier
|
654
941
|
```
|
655
942
|
|
656
|
-
|
943
|
+
NOTE: You will need an [ExecJS](https://github.com/sstephenson/execjs#readme)
|
944
|
+
supported runtime in order to use `uglifier`. If you are using Mac OS X or
|
945
|
+
Windows you have a JavaScript runtime installed in your operating system.
|
657
946
|
|
658
|
-
NOTE:
|
947
|
+
NOTE: The `config.assets.compress` initialization option is no longer used in
|
948
|
+
Rails 4 to enable either CSS or JavaScript compression. Setting it will have no
|
949
|
+
effect on the application. Instead, setting `config.assets.css_compressor` and
|
950
|
+
`config.assets.js_compressor` will control compression of CSS and JavaScript
|
951
|
+
assets.
|
659
952
|
|
660
953
|
### Using Your Own Compressor
|
661
954
|
|
662
|
-
The compressor config settings for CSS and JavaScript also take any object.
|
955
|
+
The compressor config settings for CSS and JavaScript also take any object.
|
956
|
+
This object must have a `compress` method that takes a string as the sole
|
957
|
+
argument and it must return a string.
|
663
958
|
|
664
959
|
```ruby
|
665
960
|
class Transformer
|
@@ -686,31 +981,44 @@ This can be changed to something else:
|
|
686
981
|
config.assets.prefix = "/some_other_path"
|
687
982
|
```
|
688
983
|
|
689
|
-
This is a handy option if you are updating an older project that didn't use the
|
984
|
+
This is a handy option if you are updating an older project that didn't use the
|
985
|
+
asset pipeline and already uses this path or you wish to use this path for
|
986
|
+
a new resource.
|
690
987
|
|
691
988
|
### X-Sendfile Headers
|
692
989
|
|
693
|
-
The X-Sendfile header is a directive to the web server to ignore the response
|
990
|
+
The X-Sendfile header is a directive to the web server to ignore the response
|
991
|
+
from the application, and instead serve a specified file from disk. This option
|
992
|
+
is off by default, but can be enabled if your server supports it. When enabled,
|
993
|
+
this passes responsibility for serving the file to the web server, which is
|
994
|
+
faster.
|
694
995
|
|
695
|
-
Apache and nginx support this option, which can be enabled in
|
996
|
+
Apache and nginx support this option, which can be enabled in
|
997
|
+
`config/environments/production.rb`:
|
696
998
|
|
697
999
|
```ruby
|
698
1000
|
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
|
699
1001
|
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
|
700
1002
|
```
|
701
1003
|
|
702
|
-
WARNING: If you are upgrading an existing application and intend to use this
|
1004
|
+
WARNING: If you are upgrading an existing application and intend to use this
|
1005
|
+
option, take care to paste this configuration option only into `production.rb`
|
1006
|
+
and any other environments you define with production behavior (not
|
1007
|
+
`application.rb`).
|
703
1008
|
|
704
1009
|
Assets Cache Store
|
705
1010
|
------------------
|
706
1011
|
|
707
|
-
The default Rails cache store will be used by Sprockets to cache assets in
|
1012
|
+
The default Rails cache store will be used by Sprockets to cache assets in
|
1013
|
+
development and production. This can be changed by setting
|
1014
|
+
`config.assets.cache_store`:
|
708
1015
|
|
709
1016
|
```ruby
|
710
1017
|
config.assets.cache_store = :memory_store
|
711
1018
|
```
|
712
1019
|
|
713
|
-
The options accepted by the assets cache store are the same as the application's
|
1020
|
+
The options accepted by the assets cache store are the same as the application's
|
1021
|
+
cache store.
|
714
1022
|
|
715
1023
|
```ruby
|
716
1024
|
config.assets.cache_store = :memory_store, { size: 32.megabytes }
|
@@ -721,25 +1029,35 @@ Adding Assets to Your Gems
|
|
721
1029
|
|
722
1030
|
Assets can also come from external sources in the form of gems.
|
723
1031
|
|
724
|
-
A good example of this is the `jquery-rails` gem which comes with Rails as the
|
1032
|
+
A good example of this is the `jquery-rails` gem which comes with Rails as the
|
1033
|
+
standard JavaScript library gem. This gem contains an engine class which
|
1034
|
+
inherits from `Rails::Engine`. By doing this, Rails is informed that the
|
1035
|
+
directory for this gem may contain assets and the `app/assets`, `lib/assets` and
|
1036
|
+
`vendor/assets` directories of this engine are added to the search path of
|
1037
|
+
Sprockets.
|
725
1038
|
|
726
1039
|
Making Your Library or Gem a Pre-Processor
|
727
1040
|
------------------------------------------
|
728
1041
|
|
729
1042
|
As Sprockets uses [Tilt](https://github.com/rtomayko/tilt) as a generic
|
730
|
-
interface to different templating engines, your gem should just
|
731
|
-
|
732
|
-
|
733
|
-
|
1043
|
+
interface to different templating engines, your gem should just implement the
|
1044
|
+
Tilt template protocol. Normally, you would subclass `Tilt::Template` and
|
1045
|
+
reimplement the `prepare` method, which initializes your template, and the
|
1046
|
+
`evaluate` method, which returns the processed source. The original source is
|
1047
|
+
stored in `data`. Have a look at
|
734
1048
|
[`Tilt::Template`](https://github.com/rtomayko/tilt/blob/master/lib/tilt/template.rb)
|
735
1049
|
sources to learn more.
|
736
1050
|
|
737
1051
|
```ruby
|
738
1052
|
module BangBang
|
739
1053
|
class Template < ::Tilt::Template
|
1054
|
+
def prepare
|
1055
|
+
# Do any initialization here
|
1056
|
+
end
|
1057
|
+
|
740
1058
|
# Adds a "!" to original template.
|
741
1059
|
def evaluate(scope, locals, &block)
|
742
|
-
"#{
|
1060
|
+
"#{data}!"
|
743
1061
|
end
|
744
1062
|
end
|
745
1063
|
end
|
@@ -755,31 +1073,30 @@ Sprockets.register_engine '.bang', BangBang::Template
|
|
755
1073
|
Upgrading from Old Versions of Rails
|
756
1074
|
------------------------------------
|
757
1075
|
|
758
|
-
There are a few issues when upgrading
|
1076
|
+
There are a few issues when upgrading from Rails 3.0 or Rails 2.x. The first is
|
1077
|
+
moving the files from `public/` to the new locations. See [Asset
|
1078
|
+
Organization](#asset-organization) above for guidance on the correct locations
|
1079
|
+
for different file types.
|
759
1080
|
|
760
|
-
Next will be avoiding duplicate JavaScript files. Since jQuery is the default
|
1081
|
+
Next will be avoiding duplicate JavaScript files. Since jQuery is the default
|
1082
|
+
JavaScript library from Rails 3.1 onwards, you don't need to copy `jquery.js`
|
1083
|
+
into `app/assets` and it will be included automatically.
|
761
1084
|
|
762
|
-
The third is updating the various environment files with the correct default
|
1085
|
+
The third is updating the various environment files with the correct default
|
1086
|
+
options.
|
763
1087
|
|
764
1088
|
In `application.rb`:
|
765
1089
|
|
766
1090
|
```ruby
|
767
|
-
# Enable the asset pipeline
|
768
|
-
config.assets.enabled = true
|
769
|
-
|
770
1091
|
# Version of your assets, change this if you want to expire all your assets
|
771
1092
|
config.assets.version = '1.0'
|
772
1093
|
|
773
|
-
# Change the path that assets are served from
|
774
|
-
# config.assets.prefix = "/assets"
|
1094
|
+
# Change the path that assets are served from config.assets.prefix = "/assets"
|
775
1095
|
```
|
776
1096
|
|
777
1097
|
In `development.rb`:
|
778
1098
|
|
779
1099
|
```ruby
|
780
|
-
# Do not compress assets
|
781
|
-
config.assets.compress = false
|
782
|
-
|
783
1100
|
# Expands the lines which load the assets
|
784
1101
|
config.assets.debug = true
|
785
1102
|
```
|
@@ -787,50 +1104,28 @@ config.assets.debug = true
|
|
787
1104
|
And in `production.rb`:
|
788
1105
|
|
789
1106
|
```ruby
|
790
|
-
#
|
791
|
-
config.assets.
|
792
|
-
|
793
|
-
# Choose the compressors to use
|
794
|
-
# config.assets.js_compressor = :uglifier
|
795
|
-
# config.assets.css_compressor = :yui
|
1107
|
+
# Choose the compressors to use (if any) config.assets.js_compressor =
|
1108
|
+
# :uglifier config.assets.css_compressor = :yui
|
796
1109
|
|
797
1110
|
# Don't fallback to assets pipeline if a precompiled asset is missed
|
798
1111
|
config.assets.compile = false
|
799
1112
|
|
800
|
-
# Generate digests for assets URLs.
|
1113
|
+
# Generate digests for assets URLs. This is planned for deprecation.
|
801
1114
|
config.assets.digest = true
|
802
1115
|
|
803
|
-
# Precompile additional assets (application.js, application.css, and all
|
804
|
-
# config.assets.precompile += %w( search.js )
|
1116
|
+
# Precompile additional assets (application.js, application.css, and all
|
1117
|
+
# non-JS/CSS are already added) config.assets.precompile += %w( search.js )
|
805
1118
|
```
|
806
1119
|
|
807
|
-
|
1120
|
+
Rails 4 no longer sets default config values for Sprockets in `test.rb`, so
|
1121
|
+
`test.rb` now requies Sprockets configuration. The old defaults in the test
|
1122
|
+
environment are: `config.assets.compile = true`, `config.assets.compress =
|
1123
|
+
false`, `config.assets.debug = false` and `config.assets.digest = false`.
|
808
1124
|
|
809
1125
|
The following should also be added to `Gemfile`:
|
810
1126
|
|
811
1127
|
```ruby
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
gem 'sass-rails', "~> 3.2.3"
|
816
|
-
gem 'coffee-rails', "~> 3.2.1"
|
817
|
-
gem 'uglifier'
|
818
|
-
end
|
819
|
-
```
|
820
|
-
|
821
|
-
If you use the `assets` group with Bundler, please make sure that your `config/application.rb` has the following Bundler require statement:
|
822
|
-
|
823
|
-
```ruby
|
824
|
-
# If you precompile assets before deploying to production, use this line
|
825
|
-
Bundler.require *Rails.groups(:assets => %w(development test))
|
826
|
-
# If you want your assets lazily compiled in production, use this line
|
827
|
-
# Bundler.require(:default, :assets, Rails.env)
|
828
|
-
```
|
829
|
-
|
830
|
-
Instead of the generated version:
|
831
|
-
|
832
|
-
```ruby
|
833
|
-
# Require the gems listed in Gemfile, including any gems
|
834
|
-
# you've limited to :test, :development, or :production.
|
835
|
-
Bundler.require(:default, Rails.env)
|
1128
|
+
gem 'sass-rails', "~> 3.2.3"
|
1129
|
+
gem 'coffee-rails', "~> 3.2.1"
|
1130
|
+
gem 'uglifier'
|
836
1131
|
```
|