rails 4.0.13 → 4.1.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -17
- data/guides/CHANGELOG.md +68 -34
- 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/article_with_comments.png +0 -0
- data/guides/assets/images/getting_started/challenge.png +0 -0
- data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
- data/guides/assets/images/getting_started/form_with_errors.png +0 -0
- data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/assets/images/getting_started/new_article.png +0 -0
- data/guides/assets/images/getting_started/rails_welcome.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/show_action_for_articles.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_articles.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 +9 -4
- data/guides/bug_report_templates/action_controller_master.rb +4 -2
- data/guides/bug_report_templates/active_record_gem.rb +5 -2
- data/guides/bug_report_templates/active_record_master.rb +2 -1
- data/guides/bug_report_templates/generic_gem.rb +15 -0
- data/guides/bug_report_templates/generic_master.rb +26 -0
- data/guides/code/getting_started/Gemfile +21 -24
- data/guides/code/getting_started/Gemfile.lock +78 -73
- data/guides/code/getting_started/Rakefile +1 -1
- data/guides/code/getting_started/app/assets/javascripts/application.js +1 -2
- data/guides/code/getting_started/app/views/layouts/application.html.erb +2 -2
- data/guides/code/getting_started/config/environment.rb +1 -1
- data/guides/code/getting_started/config/environments/development.rb +2 -2
- data/guides/code/getting_started/config/environments/production.rb +3 -3
- data/guides/code/getting_started/config/environments/test.rb +2 -2
- data/guides/code/getting_started/config/initializers/secret_token.rb +1 -1
- data/guides/code/getting_started/config/initializers/session_store.rb +1 -1
- data/guides/code/getting_started/config/routes.rb +1 -1
- data/guides/code/getting_started/config.ru +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/code/getting_started/test/test_helper.rb +0 -3
- data/guides/rails_guides/helpers.rb +3 -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 +2 -3
- data/guides/source/3_1_release_notes.md +2 -2
- 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 +731 -0
- data/guides/source/_welcome.html.erb +5 -2
- data/guides/source/action_controller_overview.md +189 -40
- data/guides/source/action_mailer_basics.md +27 -27
- data/guides/source/action_view_overview.md +131 -20
- 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 +93 -51
- data/guides/source/active_record_validations.md +26 -24
- data/guides/source/active_support_core_extensions.md +72 -118
- data/guides/source/active_support_instrumentation.md +13 -4
- data/guides/source/api_documentation_guidelines.md +104 -6
- data/guides/source/asset_pipeline.md +573 -244
- data/guides/source/association_basics.md +94 -22
- data/guides/source/caching_with_rails.md +15 -6
- data/guides/source/command_line.md +55 -46
- data/guides/source/configuring.md +248 -52
- data/guides/source/contributing_to_ruby_on_rails.md +18 -17
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +39 -8
- data/guides/source/development_dependencies_install.md +91 -8
- data/guides/source/documents.yaml +4 -0
- data/guides/source/engines.md +678 -232
- data/guides/source/form_helpers.md +53 -35
- data/guides/source/generators.md +19 -15
- data/guides/source/getting_started.md +758 -497
- data/guides/source/i18n.md +64 -28
- data/guides/source/index.html.erb +1 -1
- data/guides/source/initialization.md +155 -58
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +2 -2
- data/guides/source/layouts_and_rendering.md +59 -26
- data/guides/source/maintenance_policy.md +3 -3
- data/guides/source/migrations.md +101 -62
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +34 -31
- data/guides/source/rails_application_templates.md +27 -8
- data/guides/source/rails_on_rack.md +41 -58
- data/guides/source/routing.md +115 -104
- data/guides/source/ruby_on_rails_guides_guidelines.md +2 -2
- data/guides/source/security.md +81 -36
- data/guides/source/testing.md +56 -79
- data/guides/source/upgrading_ruby_on_rails.md +531 -21
- data/guides/source/working_with_javascript_in_rails.md +19 -11
- metadata +51 -23
- 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/post_with_comments.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/assets/images/getting_started/undefined_method_post_path.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/jaimeiniesta.jpg +0 -0
- data/guides/source/kindle/KINDLE.md +0 -26
@@ -24,15 +24,514 @@ Rails generally stays close to the latest released Ruby version when it's releas
|
|
24
24
|
|
25
25
|
TIP: Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails. Ruby Enterprise Edition has these fixed since the release of 1.8.7-2010.02. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x, jump straight to 1.9.3 for smooth sailing.
|
26
26
|
|
27
|
-
Upgrading from Rails
|
27
|
+
Upgrading from Rails 4.0 to Rails 4.1
|
28
28
|
-------------------------------------
|
29
29
|
|
30
|
-
|
30
|
+
### CSRF protection from remote `<script>` tags
|
31
|
+
|
32
|
+
Or, "whaaat my tests are failing!!!?"
|
33
|
+
|
34
|
+
Cross-site request forgery (CSRF) protection now covers GET requests with
|
35
|
+
JavaScript responses, too. That prevents a third-party site from referencing
|
36
|
+
your JavaScript URL and attempting to run it to extract sensitive data.
|
37
|
+
|
38
|
+
This means that your functional and integration tests that use
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
get :index, format: :js
|
42
|
+
```
|
43
|
+
|
44
|
+
will now trigger CSRF protection. Switch to
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
xhr :get, :index, format: :js
|
48
|
+
```
|
49
|
+
|
50
|
+
to explicitly test an XmlHttpRequest.
|
51
|
+
|
52
|
+
If you really mean to load JavaScript from remote `<script>` tags, skip CSRF
|
53
|
+
protection on that action.
|
54
|
+
|
55
|
+
### Spring
|
56
|
+
|
57
|
+
If you want to use Spring as your application preloader you need to:
|
58
|
+
|
59
|
+
1. Add `gem 'spring', group: :development` to your `Gemfile`.
|
60
|
+
2. Install spring using `bundle install`.
|
61
|
+
3. Springify your binstubs with `bundle exec spring binstub --all`.
|
62
|
+
|
63
|
+
NOTE: User defined rake tasks will run in the `development` environment by
|
64
|
+
default. If you want them to run in other environments consult the
|
65
|
+
[Spring README](https://github.com/rails/spring#rake).
|
66
|
+
|
67
|
+
### `config/secrets.yml`
|
68
|
+
|
69
|
+
If you want to use the new `secrets.yml` convention to store your application's
|
70
|
+
secrets, you need to:
|
71
|
+
|
72
|
+
1. Create a `secrets.yml` file in your `config` folder with the following content:
|
73
|
+
|
74
|
+
```yaml
|
75
|
+
development:
|
76
|
+
secret_key_base:
|
77
|
+
|
78
|
+
test:
|
79
|
+
secret_key_base:
|
80
|
+
|
81
|
+
production:
|
82
|
+
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
|
83
|
+
```
|
84
|
+
|
85
|
+
2. Use your existing `secret_key_base` from the `secret_token.rb` initializer to
|
86
|
+
set the SECRET_KEY_BASE environment variable for whichever users run the Rails
|
87
|
+
app in production mode. Alternately, you can simply copy the existing
|
88
|
+
`secret_key_base` from the `secret_token.rb` initializer to `secrets.yml`
|
89
|
+
under the `production` section, replacing '<%= ENV["SECRET_KEY_BASE"] %>'.
|
90
|
+
|
91
|
+
3. Remove the `secret_token.rb` initializer.
|
92
|
+
|
93
|
+
4. Use `rake secret` to generate new keys for the `development` and `test` sections.
|
94
|
+
|
95
|
+
5. Restart your server.
|
96
|
+
|
97
|
+
### Changes to test helper
|
98
|
+
|
99
|
+
If your test helper contains a call to
|
100
|
+
`ActiveRecord::Migration.check_pending!` this can be removed. The check
|
101
|
+
is now done automatically when you `require 'test_help'`, although
|
102
|
+
leaving this line in your helper is not harmful in any way.
|
103
|
+
|
104
|
+
### Cookies serializer
|
105
|
+
|
106
|
+
Applications created before Rails 4.1 uses `Marshal` to serialize cookie values into
|
107
|
+
the signed and encrypted cookie jars. If you want to use the new `JSON`-based format
|
108
|
+
in your application, you can add an initializer file with the following content:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
Rails.application.config.action_dispatch.cookies_serializer = :hybrid
|
112
|
+
```
|
113
|
+
|
114
|
+
This would transparently migrate your existing `Marshal`-serialized cookies into the
|
115
|
+
new `JSON`-based format.
|
116
|
+
|
117
|
+
When using the `:json` or `:hybrid` serializer, you should beware that not all
|
118
|
+
Ruby objects can be serialized as JSON. For example, `Date` and `Time` objects
|
119
|
+
will be serialized as strings, and `Hash`es will have their keys stringified.
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
class CookiesController < ApplicationController
|
123
|
+
def set_cookie
|
124
|
+
cookies.encrypted[:expiration_date] = Date.tomorrow # => Thu, 20 Mar 2014
|
125
|
+
redirect_to action: 'read_cookie'
|
126
|
+
end
|
127
|
+
|
128
|
+
def read_cookie
|
129
|
+
cookies.encrypted[:expiration_date] # => "2014-03-20"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
It's advisable that you only store simple data (strings and numbers) in cookies.
|
135
|
+
If you have to store complex objects, you would need to handle the conversion
|
136
|
+
manually when reading the values on subsequent requests.
|
137
|
+
|
138
|
+
If you use the cookie session store, this would apply to the `session` and
|
139
|
+
`flash` hash as well.
|
140
|
+
|
141
|
+
### Flash structure changes
|
142
|
+
|
143
|
+
Flash message keys are
|
144
|
+
[normalized to strings](https://github.com/rails/rails/commit/a668beffd64106a1e1fedb71cc25eaaa11baf0c1). They
|
145
|
+
can still be accessed using either symbols or strings. Lopping through the flash
|
146
|
+
will always yield string keys:
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
flash["string"] = "a string"
|
150
|
+
flash[:symbol] = "a symbol"
|
151
|
+
|
152
|
+
# Rails < 4.1
|
153
|
+
flash.keys # => ["string", :symbol]
|
154
|
+
|
155
|
+
# Rails >= 4.1
|
156
|
+
flash.keys # => ["string", "symbol"]
|
157
|
+
```
|
158
|
+
|
159
|
+
Make sure you are comparing Flash message keys against strings.
|
160
|
+
|
161
|
+
### Changes in JSON handling
|
162
|
+
|
163
|
+
There are a few major changes related to JSON handling in Rails 4.1.
|
164
|
+
|
165
|
+
#### MultiJSON removal
|
166
|
+
|
167
|
+
MultiJSON has reached its [end-of-life](https://github.com/rails/rails/pull/10576)
|
168
|
+
and has been removed from Rails.
|
169
|
+
|
170
|
+
If your application currently depend on MultiJSON directly, you have a few options:
|
171
|
+
|
172
|
+
1. Add 'multi_json' to your Gemfile. Note that this might cease to work in the future
|
173
|
+
|
174
|
+
2. Migrate away from MultiJSON by using `obj.to_json`, and `JSON.parse(str)` instead.
|
175
|
+
|
176
|
+
WARNING: Do not simply replace `MultiJson.dump` and `MultiJson.load` with
|
177
|
+
`JSON.dump` and `JSON.load`. These JSON gem APIs are meant for serializing and
|
178
|
+
deserializing arbitrary Ruby objects and are generally [unsafe](http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html#method-i-load).
|
179
|
+
|
180
|
+
#### JSON gem compatibility
|
181
|
+
|
182
|
+
Historically, Rails had some compatibility issues with the JSON gem. Using
|
183
|
+
`JSON.generate` and `JSON.dump` inside a Rails application could produce
|
184
|
+
unexpected errors.
|
185
|
+
|
186
|
+
Rails 4.1 fixed these issues by isolating its own encoder from the JSON gem. The
|
187
|
+
JSON gem APIs will function as normal, but they will not have access to any
|
188
|
+
Rails-specific features. For example:
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
class FooBar
|
192
|
+
def as_json(options = nil)
|
193
|
+
{ foo: 'bar' }
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
>> FooBar.new.to_json # => "{\"foo\":\"bar\"}"
|
198
|
+
>> JSON.generate(FooBar.new, quirks_mode: true) # => "\"#<FooBar:0x007fa80a481610>\""
|
199
|
+
```
|
200
|
+
|
201
|
+
#### New JSON encoder
|
202
|
+
|
203
|
+
The JSON encoder in Rails 4.1 has been rewritten to take advantage of the JSON
|
204
|
+
gem. For most applications, this should be a transparent change. However, as
|
205
|
+
part of the rewrite, the following features have been removed from the encoder:
|
206
|
+
|
207
|
+
1. Circular data structure detection
|
208
|
+
2. Support for the `encode_json` hook
|
209
|
+
3. Option to encode `BigDecimal` objects as numbers instead of strings
|
210
|
+
|
211
|
+
If your application depends on one of these features, you can get them back by
|
212
|
+
adding the [`activesupport-json_encoder`](https://github.com/rails/activesupport-json_encoder)
|
213
|
+
gem to your Gemfile.
|
214
|
+
|
215
|
+
#### JSON representation of Time objects
|
216
|
+
|
217
|
+
`#as_json` for objects with time component (`Time`, `DateTime`, `ActiveSupport::TimeWithZone`)
|
218
|
+
now returns millisecond precision by default. If you need to keep old behavior with no millisecond
|
219
|
+
precision, set the following in an initializer:
|
220
|
+
|
221
|
+
```
|
222
|
+
ActiveSupport::JSON::Encoding.time_precision = 0
|
223
|
+
```
|
224
|
+
|
225
|
+
### Usage of `return` within inline callback blocks
|
226
|
+
|
227
|
+
Previously, Rails allowed inline callback blocks to use `return` this way:
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
class ReadOnlyModel < ActiveRecord::Base
|
231
|
+
before_save { return false } # BAD
|
232
|
+
end
|
233
|
+
```
|
234
|
+
|
235
|
+
This behaviour was never intentionally supported. Due to a change in the internals
|
236
|
+
of `ActiveSupport::Callbacks`, this is no longer allowed in Rails 4.1. Using a
|
237
|
+
`return` statement in an inline callback block causes a `LocalJumpError` to
|
238
|
+
be raised when the callback is executed.
|
239
|
+
|
240
|
+
Inline callback blocks using `return` can be refactored to evaluate to the
|
241
|
+
returned value:
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
class ReadOnlyModel < ActiveRecord::Base
|
245
|
+
before_save { false } # GOOD
|
246
|
+
end
|
247
|
+
```
|
248
|
+
|
249
|
+
Alternatively, if `return` is preferred it is recommended to explicitly define
|
250
|
+
a method:
|
251
|
+
|
252
|
+
```ruby
|
253
|
+
class ReadOnlyModel < ActiveRecord::Base
|
254
|
+
before_save :before_save_callback # GOOD
|
255
|
+
|
256
|
+
private
|
257
|
+
def before_save_callback
|
258
|
+
return false
|
259
|
+
end
|
260
|
+
end
|
261
|
+
```
|
262
|
+
|
263
|
+
This change applies to most places in Rails where callbacks are used, including
|
264
|
+
Active Record and Active Model callbacks, as well as filters in Action
|
265
|
+
Controller (e.g. `before_action`).
|
266
|
+
|
267
|
+
See [this pull request](https://github.com/rails/rails/pull/13271) for more
|
268
|
+
details.
|
269
|
+
|
270
|
+
### Methods defined in Active Record fixtures
|
271
|
+
|
272
|
+
Rails 4.1 evaluates each fixture's ERB in a separate context, so helper methods
|
273
|
+
defined in a fixture will not be available in other fixtures.
|
274
|
+
|
275
|
+
Helper methods that are used in multiple fixtures should be defined on modules
|
276
|
+
included in the newly introduced `ActiveRecord::FixtureSet.context_class`, in
|
277
|
+
`test_helper.rb`.
|
278
|
+
|
279
|
+
```ruby
|
280
|
+
module FixtureFileHelpers
|
281
|
+
def file_sha(path)
|
282
|
+
Digest::SHA2.hexdigest(File.read(Rails.root.join('test/fixtures', path)))
|
283
|
+
end
|
284
|
+
end
|
285
|
+
ActiveRecord::FixtureSet.context_class.send :include, FixtureFileHelpers
|
286
|
+
```
|
287
|
+
|
288
|
+
### I18n enforcing available locales
|
289
|
+
|
290
|
+
Rails 4.1 now defaults the I18n option `enforce_available_locales` to `true`,
|
291
|
+
meaning that it will make sure that all locales passed to it must be declared in
|
292
|
+
the `available_locales` list.
|
293
|
+
|
294
|
+
To disable it (and allow I18n to accept *any* locale option) add the following
|
295
|
+
configuration to your application:
|
296
|
+
|
297
|
+
```ruby
|
298
|
+
config.i18n.enforce_available_locales = false
|
299
|
+
```
|
300
|
+
|
301
|
+
Note that this option was added as a security measure, to ensure user input could
|
302
|
+
not be used as locale information unless previously known, so it's recommended not
|
303
|
+
to disable this option unless you have a strong reason for doing so.
|
304
|
+
|
305
|
+
### Mutator methods called on Relation
|
306
|
+
|
307
|
+
`Relation` no longer has mutator methods like `#map!` and `#delete_if`. Convert
|
308
|
+
to an `Array` by calling `#to_a` before using these methods.
|
309
|
+
|
310
|
+
It intends to prevent odd bugs and confusion in code that call mutator
|
311
|
+
methods directly on the `Relation`.
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
# Instead of this
|
315
|
+
Author.where(name: 'Hank Moody').compact!
|
316
|
+
|
317
|
+
# Now you have to do this
|
318
|
+
authors = Author.where(name: 'Hank Moody').to_a
|
319
|
+
authors.compact!
|
320
|
+
```
|
321
|
+
|
322
|
+
### Changes on Default Scopes
|
323
|
+
|
324
|
+
Default scopes are no longer overriden by chained conditions.
|
325
|
+
|
326
|
+
In previous versions when you defined a `default_scope` in a model
|
327
|
+
it was overriden by chained conditions in the same field. Now it
|
328
|
+
is merged like any other scope.
|
329
|
+
|
330
|
+
Before:
|
331
|
+
|
332
|
+
```ruby
|
333
|
+
class User < ActiveRecord::Base
|
334
|
+
default_scope { where state: 'pending' }
|
335
|
+
scope :active, -> { where state: 'active' }
|
336
|
+
scope :inactive, -> { where state: 'inactive' }
|
337
|
+
end
|
338
|
+
|
339
|
+
User.all
|
340
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
|
341
|
+
|
342
|
+
User.active
|
343
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'active'
|
344
|
+
|
345
|
+
User.where(state: 'inactive')
|
346
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'inactive'
|
347
|
+
```
|
348
|
+
|
349
|
+
After:
|
350
|
+
|
351
|
+
```ruby
|
352
|
+
class User < ActiveRecord::Base
|
353
|
+
default_scope { where state: 'pending' }
|
354
|
+
scope :active, -> { where state: 'active' }
|
355
|
+
scope :inactive, -> { where state: 'inactive' }
|
356
|
+
end
|
357
|
+
|
358
|
+
User.all
|
359
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
|
360
|
+
|
361
|
+
User.active
|
362
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending' AND "users"."state" = 'active'
|
363
|
+
|
364
|
+
User.where(state: 'inactive')
|
365
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending' AND "users"."state" = 'inactive'
|
366
|
+
```
|
367
|
+
|
368
|
+
To get the previous behavior it is needed to explicitly remove the
|
369
|
+
`default_scope` condition using `unscoped`, `unscope`, `rewhere` or
|
370
|
+
`except`.
|
371
|
+
|
372
|
+
```ruby
|
373
|
+
class User < ActiveRecord::Base
|
374
|
+
default_scope { where state: 'pending' }
|
375
|
+
scope :active, -> { unscope(where: :state).where(state: 'active') }
|
376
|
+
scope :inactive, -> { rewhere state: 'inactive' }
|
377
|
+
end
|
378
|
+
|
379
|
+
User.all
|
380
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
|
381
|
+
|
382
|
+
User.active
|
383
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'active'
|
384
|
+
|
385
|
+
User.inactive
|
386
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'inactive'
|
387
|
+
```
|
388
|
+
|
389
|
+
### Rendering content from string
|
390
|
+
|
391
|
+
Rails 4.1 introduces `:plain`, `:html`, and `:body` options to `render`. Those
|
392
|
+
options are now the preferred way to render string-based content, as it allows
|
393
|
+
you to specify which content type you want the response sent as.
|
394
|
+
|
395
|
+
* `render :plain` will set the content type to `text/plain`
|
396
|
+
* `render :html` will set the content type to `text/html`
|
397
|
+
* `render :body` will *not* set the content type header.
|
398
|
+
|
399
|
+
From the security standpoint, if you don't expect to have any markup in your
|
400
|
+
response body, you should be using `render :plain` as most browsers will escape
|
401
|
+
unsafe content in the response for you.
|
402
|
+
|
403
|
+
We will be deprecating the use of `render :text` in a future version. So please
|
404
|
+
start using the more precise `:plain:`, `:html`, and `:body` options instead.
|
405
|
+
Using `render :text` may pose a security risk, as the content is sent as
|
406
|
+
`text/html`.
|
407
|
+
|
408
|
+
### PostgreSQL json and hstore datatypes
|
409
|
+
|
410
|
+
Rails 4.1 will map `json` and `hstore` columns to a string-keyed Ruby `Hash`.
|
411
|
+
In earlier versions a `HashWithIndifferentAccess` was used. This means that
|
412
|
+
symbol access is no longer supported. This is also the case for
|
413
|
+
`store_accessors` based on top of `json` or `hstore` columns. Make sure to use
|
414
|
+
string keys consistently.
|
415
|
+
|
416
|
+
### Explicit block use for `ActiveSupport::Callbacks`
|
417
|
+
|
418
|
+
Rails 4.1 now expects an explicit block to be passed when calling
|
419
|
+
`ActiveSupport::Callbacks.set_callback`. This change stems from
|
420
|
+
`ActiveSupport::Callbacks` being largely rewritten for the 4.1 release.
|
421
|
+
|
422
|
+
```ruby
|
423
|
+
# Previously in Rails 4.0
|
424
|
+
set_callback :save, :around, ->(r, &block) { stuff; result = block.call; stuff }
|
425
|
+
|
426
|
+
# Now in Rails 4.1
|
427
|
+
set_callback :save, :around, ->(r, block) { stuff; result = block.call; stuff }
|
428
|
+
```
|
429
|
+
|
430
|
+
Upgrading from Rails 3.2 to Rails 4.0
|
431
|
+
-------------------------------------
|
31
432
|
|
32
433
|
If your application is currently on any version of Rails older than 3.2.x, you should upgrade to Rails 3.2 before attempting one to Rails 4.0.
|
33
434
|
|
34
435
|
The following changes are meant for upgrading your application to Rails 4.0.
|
35
436
|
|
437
|
+
### HTTP PATCH
|
438
|
+
|
439
|
+
Rails 4 now uses `PATCH` as the primary HTTP verb for updates when a RESTful
|
440
|
+
resource is declared in `config/routes.rb`. The `update` action is still used,
|
441
|
+
and `PUT` requests will continue to be routed to the `update` action as well.
|
442
|
+
So, if you're using only the standard RESTful routes, no changes need to be made:
|
443
|
+
|
444
|
+
```ruby
|
445
|
+
resources :users
|
446
|
+
```
|
447
|
+
|
448
|
+
```erb
|
449
|
+
<%= form_for @user do |f| %>
|
450
|
+
```
|
451
|
+
|
452
|
+
```ruby
|
453
|
+
class UsersController < ApplicationController
|
454
|
+
def update
|
455
|
+
# No change needed; PATCH will be preferred, and PUT will still work.
|
456
|
+
end
|
457
|
+
end
|
458
|
+
```
|
459
|
+
|
460
|
+
However, you will need to make a change if you are using `form_for` to update
|
461
|
+
a resource in conjunction with a custom route using the `PUT` HTTP method:
|
462
|
+
|
463
|
+
```ruby
|
464
|
+
resources :users, do
|
465
|
+
put :update_name, on: :member
|
466
|
+
end
|
467
|
+
```
|
468
|
+
|
469
|
+
```erb
|
470
|
+
<%= form_for [ :update_name, @user ] do |f| %>
|
471
|
+
```
|
472
|
+
|
473
|
+
```ruby
|
474
|
+
class UsersController < ApplicationController
|
475
|
+
def update_name
|
476
|
+
# Change needed; form_for will try to use a non-existent PATCH route.
|
477
|
+
end
|
478
|
+
end
|
479
|
+
```
|
480
|
+
|
481
|
+
If the action is not being used in a public API and you are free to change the
|
482
|
+
HTTP method, you can update your route to use `patch` instead of `put`:
|
483
|
+
|
484
|
+
`PUT` requests to `/users/:id` in Rails 4 get routed to `update` as they are
|
485
|
+
today. So, if you have an API that gets real PUT requests it is going to work.
|
486
|
+
The router also routes `PATCH` requests to `/users/:id` to the `update` action.
|
487
|
+
|
488
|
+
```ruby
|
489
|
+
resources :users do
|
490
|
+
patch :update_name, on: :member
|
491
|
+
end
|
492
|
+
```
|
493
|
+
|
494
|
+
If the action is being used in a public API and you can't change to HTTP method
|
495
|
+
being used, you can update your form to use the `PUT` method instead:
|
496
|
+
|
497
|
+
```erb
|
498
|
+
<%= form_for [ :update_name, @user ], method: :put do |f| %>
|
499
|
+
```
|
500
|
+
|
501
|
+
For more on PATCH and why this change was made, see [this post](http://weblog.rubyonrails.org/2012/2/26/edge-rails-patch-is-the-new-primary-http-method-for-updates/)
|
502
|
+
on the Rails blog.
|
503
|
+
|
504
|
+
#### A note about media types
|
505
|
+
|
506
|
+
The errata for the `PATCH` verb [specifies that a 'diff' media type should be
|
507
|
+
used with `PATCH`](http://www.rfc-editor.org/errata_search.php?rfc=5789). One
|
508
|
+
such format is [JSON Patch](http://tools.ietf.org/html/rfc6902). While Rails
|
509
|
+
does not support JSON Patch natively, it's easy enough to add support:
|
510
|
+
|
511
|
+
```
|
512
|
+
# in your controller
|
513
|
+
def update
|
514
|
+
respond_to do |format|
|
515
|
+
format.json do
|
516
|
+
# perform a partial update
|
517
|
+
@post.update params[:post]
|
518
|
+
end
|
519
|
+
|
520
|
+
format.json_patch do
|
521
|
+
# perform sophisticated change
|
522
|
+
end
|
523
|
+
end
|
524
|
+
end
|
525
|
+
|
526
|
+
# In config/initializers/json_patch.rb:
|
527
|
+
Mime::Type.register 'application/json-patch+json', :json_patch
|
528
|
+
```
|
529
|
+
|
530
|
+
As JSON Patch was only recently made into an RFC, there aren't a lot of great
|
531
|
+
Ruby libraries yet. Aaron Patterson's
|
532
|
+
[hana](https://github.com/tenderlove/hana) is one such gem, but doesn't have
|
533
|
+
full support for the last few changes in the specification.
|
534
|
+
|
36
535
|
### Gemfile
|
37
536
|
|
38
537
|
Rails 4.0 removed the `assets` group from Gemfile. You'd need to remove that
|
@@ -42,7 +541,7 @@ file (in `config/application.rb`):
|
|
42
541
|
```ruby
|
43
542
|
# Require the gems listed in Gemfile, including any gems
|
44
543
|
# you've limited to :test, :development, or :production.
|
45
|
-
Bundler.require(
|
544
|
+
Bundler.require(*Rails.groups)
|
46
545
|
```
|
47
546
|
|
48
547
|
### vendor/plugins
|
@@ -77,6 +576,7 @@ this gem such as `whitelist_attributes` or `mass_assignment_sanitizer` options.
|
|
77
576
|
```
|
78
577
|
|
79
578
|
* Rails 4.0 has deprecated `ActiveRecord::Fixtures` in favor of `ActiveRecord::FixtureSet`.
|
579
|
+
|
80
580
|
* Rails 4.0 has deprecated `ActiveRecord::TestCase` in favor of `ActiveSupport::TestCase`.
|
81
581
|
|
82
582
|
* Rails 4.0 has deprecated the old-style hash based finder API. This means that
|
@@ -91,6 +591,12 @@ this gem such as `whitelist_attributes` or `mass_assignment_sanitizer` options.
|
|
91
591
|
* `find_or_initialize_by_...` becomes `find_or_initialize_by(...)`.
|
92
592
|
* `find_or_create_by_...` becomes `find_or_create_by(...)`.
|
93
593
|
|
594
|
+
* Note that `where(...)` returns a relation, not an array like the old finders. If you require an `Array`, use `where(...).to_a`.
|
595
|
+
|
596
|
+
* These equivalent methods may not execute the same SQL as the previous implementation.
|
597
|
+
|
598
|
+
* To re-enable the old finders, you can use the [activerecord-deprecated_finders gem](https://github.com/rails/activerecord-deprecated_finders).
|
599
|
+
|
94
600
|
### Active Resource
|
95
601
|
|
96
602
|
Rails 4.0 extracted Active Resource to its own gem. If you still need the feature you can add the [Active Resource gem](https://github.com/rails/activeresource) in your Gemfile.
|
@@ -178,13 +684,13 @@ get 'こんにちは', controller: 'welcome', action: 'index'
|
|
178
684
|
|
179
685
|
```ruby
|
180
686
|
# Rails 3.x
|
181
|
-
match
|
687
|
+
match '/' => 'root#index'
|
182
688
|
|
183
689
|
# becomes
|
184
|
-
match
|
690
|
+
match '/' => 'root#index', via: :get
|
185
691
|
|
186
692
|
# or
|
187
|
-
get
|
693
|
+
get '/' => 'root#index'
|
188
694
|
```
|
189
695
|
|
190
696
|
* Rails 4.0 has removed `ActionDispatch::BestStandardsSupport` middleware, `<!DOCTYPE html>` already triggers standards mode per http://msdn.microsoft.com/en-us/library/jj676915(v=vs.85).aspx and ChromeFrame header has been moved to `config.action_dispatch.default_headers`.
|
@@ -229,9 +735,8 @@ Active Record Observer and Action Controller Sweeper have been extracted to the
|
|
229
735
|
|
230
736
|
### sprockets-rails
|
231
737
|
|
232
|
-
* `assets:precompile:primary`
|
233
|
-
* The `config.assets.compress` option should be changed to
|
234
|
-
`config.assets.js_compressor` like so for instance:
|
738
|
+
* `assets:precompile:primary` and `assets:precompile:all` have been removed. Use `assets:precompile` instead.
|
739
|
+
* The `config.assets.compress` option should be changed to `config.assets.js_compressor` like so for instance:
|
235
740
|
|
236
741
|
```ruby
|
237
742
|
config.assets.js_compressor = :uglifier
|
@@ -239,25 +744,26 @@ config.assets.js_compressor = :uglifier
|
|
239
744
|
|
240
745
|
### sass-rails
|
241
746
|
|
242
|
-
* `
|
747
|
+
* `asset-url` with two arguments is deprecated. For example: `asset-url("rails.png", image)` becomes `asset-url("rails.png")`.
|
243
748
|
|
244
749
|
Upgrading from Rails 3.1 to Rails 3.2
|
245
750
|
-------------------------------------
|
246
751
|
|
247
752
|
If your application is currently on any version of Rails older than 3.1.x, you should upgrade to Rails 3.1 before attempting an update to Rails 3.2.
|
248
753
|
|
249
|
-
The following changes are meant for upgrading your application to Rails 3.2.
|
754
|
+
The following changes are meant for upgrading your application to Rails 3.2.17,
|
755
|
+
the last 3.2.x version of Rails.
|
250
756
|
|
251
757
|
### Gemfile
|
252
758
|
|
253
759
|
Make the following changes to your `Gemfile`.
|
254
760
|
|
255
761
|
```ruby
|
256
|
-
gem 'rails', '
|
762
|
+
gem 'rails', '3.2.17'
|
257
763
|
|
258
764
|
group :assets do
|
259
|
-
gem 'sass-rails', '~> 3.2.
|
260
|
-
gem 'coffee-rails', '~> 3.2.
|
765
|
+
gem 'sass-rails', '~> 3.2.6'
|
766
|
+
gem 'coffee-rails', '~> 3.2.2'
|
261
767
|
gem 'uglifier', '>= 1.0.3'
|
262
768
|
end
|
263
769
|
```
|
@@ -288,26 +794,30 @@ config.active_record.mass_assignment_sanitizer = :strict
|
|
288
794
|
|
289
795
|
Rails 3.2 deprecates `vendor/plugins` and Rails 4.0 will remove them completely. While it's not strictly necessary as part of a Rails 3.2 upgrade, you can start replacing any plugins by extracting them to gems and adding them to your Gemfile. If you choose not to make them gems, you can move them into, say, `lib/my_plugin/*` and add an appropriate initializer in `config/initializers/my_plugin.rb`.
|
290
796
|
|
797
|
+
### Active Record
|
798
|
+
|
799
|
+
Option `:dependent => :restrict` has been removed from `belongs_to`. If you want to prevent deleting the object if there are any associated objects, you can set `:dependent => :destroy` and return `false` after checking for existence of association from any of the associated object's destroy callbacks.
|
800
|
+
|
291
801
|
Upgrading from Rails 3.0 to Rails 3.1
|
292
802
|
-------------------------------------
|
293
803
|
|
294
804
|
If your application is currently on any version of Rails older than 3.0.x, you should upgrade to Rails 3.0 before attempting an update to Rails 3.1.
|
295
805
|
|
296
|
-
The following changes are meant for upgrading your application to Rails 3.1.
|
806
|
+
The following changes are meant for upgrading your application to Rails 3.1.12, the last 3.1.x version of Rails.
|
297
807
|
|
298
808
|
### Gemfile
|
299
809
|
|
300
810
|
Make the following changes to your `Gemfile`.
|
301
811
|
|
302
812
|
```ruby
|
303
|
-
gem 'rails', '
|
813
|
+
gem 'rails', '3.1.12'
|
304
814
|
gem 'mysql2'
|
305
815
|
|
306
816
|
# Needed for the new asset pipeline
|
307
817
|
group :assets do
|
308
|
-
gem 'sass-rails',
|
309
|
-
gem 'coffee-rails',
|
310
|
-
gem 'uglifier',
|
818
|
+
gem 'sass-rails', '~> 3.1.7'
|
819
|
+
gem 'coffee-rails', '~> 3.1.1'
|
820
|
+
gem 'uglifier', '>= 1.0.3'
|
311
821
|
end
|
312
822
|
|
313
823
|
# jQuery is the default JavaScript library in Rails 3.1
|
@@ -375,7 +885,7 @@ You can help test performance with these additions to your test environment:
|
|
375
885
|
```ruby
|
376
886
|
# Configure static asset server for tests with Cache-Control for performance
|
377
887
|
config.serve_static_assets = true
|
378
|
-
config.static_cache_control =
|
888
|
+
config.static_cache_control = 'public, max-age=3600'
|
379
889
|
```
|
380
890
|
|
381
891
|
### config/initializers/wrap_parameters.rb
|
@@ -410,7 +920,7 @@ AppName::Application.config.session_store :cookie_store, key: 'SOMETHINGNEW'
|
|
410
920
|
or
|
411
921
|
|
412
922
|
```bash
|
413
|
-
$ rake db:sessions:clear
|
923
|
+
$ bin/rake db:sessions:clear
|
414
924
|
```
|
415
925
|
|
416
926
|
### Remove :cache and :concat options in asset helpers references in views
|