rails 4.0.0 → 4.2.11.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +30 -23
- data/guides/CHANGELOG.md +108 -6
- data/guides/Rakefile +21 -6
- data/guides/assets/images/akshaysurve.jpg +0 -0
- 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 +36 -34
- data/guides/assets/stylesheets/main.css +6 -2
- data/guides/assets/stylesheets/print.css +1 -1
- data/guides/bug_report_templates/action_controller_gem.rb +47 -0
- data/guides/bug_report_templates/action_controller_master.rb +54 -0
- data/guides/bug_report_templates/active_record_gem.rb +5 -2
- data/guides/bug_report_templates/active_record_master.rb +3 -2
- data/guides/bug_report_templates/generic_gem.rb +15 -0
- data/guides/bug_report_templates/generic_master.rb +26 -0
- data/guides/rails_guides.rb +23 -4
- data/guides/rails_guides/generator.rb +1 -1
- data/guides/rails_guides/helpers.rb +4 -2
- data/guides/rails_guides/levenshtein.rb +27 -21
- data/guides/rails_guides/markdown.rb +11 -7
- data/guides/rails_guides/markdown/renderer.rb +1 -1
- data/guides/source/2_2_release_notes.md +3 -3
- data/guides/source/2_3_release_notes.md +12 -12
- data/guides/source/3_0_release_notes.md +10 -13
- data/guides/source/3_1_release_notes.md +7 -4
- data/guides/source/3_2_release_notes.md +17 -14
- data/guides/source/4_0_release_notes.md +110 -54
- data/guides/source/4_1_release_notes.md +730 -0
- data/guides/source/4_2_release_notes.md +877 -0
- data/guides/source/_license.html.erb +1 -1
- data/guides/source/_welcome.html.erb +6 -2
- data/guides/source/action_controller_overview.md +223 -57
- data/guides/source/action_mailer_basics.md +129 -76
- data/guides/source/action_view_overview.md +247 -246
- data/guides/source/active_job_basics.md +339 -0
- data/guides/source/active_model_basics.md +374 -20
- data/guides/source/active_record_basics.md +46 -45
- data/guides/source/active_record_callbacks.md +83 -28
- data/guides/source/{migrations.md → active_record_migrations.md} +191 -275
- data/guides/source/active_record_postgresql.md +433 -0
- data/guides/source/active_record_querying.md +382 -300
- data/guides/source/active_record_validations.md +64 -55
- data/guides/source/active_support_core_extensions.md +229 -187
- data/guides/source/active_support_instrumentation.md +23 -22
- data/guides/source/api_documentation_guidelines.md +167 -15
- data/guides/source/asset_pipeline.md +768 -294
- data/guides/source/association_basics.md +188 -96
- data/guides/source/autoloading_and_reloading_constants.md +1311 -0
- data/guides/source/caching_with_rails.md +45 -11
- data/guides/source/command_line.md +96 -65
- data/guides/source/configuring.md +404 -70
- data/guides/source/contributing_to_ruby_on_rails.md +270 -130
- data/guides/source/credits.html.erb +7 -3
- data/guides/source/debugging_rails_applications.md +471 -284
- data/guides/source/development_dependencies_install.md +115 -21
- data/guides/source/documents.yaml +31 -9
- data/guides/source/engines.md +737 -291
- data/guides/source/form_helpers.md +137 -89
- data/guides/source/generators.md +60 -28
- data/guides/source/getting_started.md +1007 -596
- data/guides/source/i18n.md +178 -96
- data/guides/source/index.html.erb +2 -1
- data/guides/source/initialization.md +248 -104
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +14 -22
- data/guides/source/layouts_and_rendering.md +78 -46
- data/guides/source/maintenance_policy.md +78 -0
- data/guides/source/nested_model_forms.md +10 -7
- data/guides/source/plugins.md +66 -57
- data/guides/source/rails_application_templates.md +49 -12
- data/guides/source/rails_on_rack.md +50 -60
- data/guides/source/routing.md +190 -139
- data/guides/source/ruby_on_rails_guides_guidelines.md +12 -13
- data/guides/source/security.md +134 -83
- data/guides/source/testing.md +322 -200
- data/guides/source/upgrading_ruby_on_rails.md +834 -37
- data/guides/source/working_with_javascript_in_rails.md +36 -26
- data/guides/w3c_validator.rb +2 -0
- metadata +93 -116
- 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/code/getting_started/Gemfile +0 -43
- data/guides/code/getting_started/Gemfile.lock +0 -150
- data/guides/code/getting_started/README.rdoc +0 -28
- data/guides/code/getting_started/Rakefile +0 -6
- data/guides/code/getting_started/app/assets/javascripts/application.js +0 -16
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -17
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -47
- data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
- data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -7
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
- data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
- data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
- data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
- data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -3
- data/guides/code/getting_started/bin/bundle +0 -4
- data/guides/code/getting_started/bin/rails +0 -4
- data/guides/code/getting_started/bin/rake +0 -4
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/config/application.rb +0 -18
- data/guides/code/getting_started/config/boot.rb +0 -4
- data/guides/code/getting_started/config/database.yml +0 -25
- data/guides/code/getting_started/config/environment.rb +0 -5
- data/guides/code/getting_started/config/environments/development.rb +0 -30
- data/guides/code/getting_started/config/environments/production.rb +0 -80
- data/guides/code/getting_started/config/environments/test.rb +0 -36
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
- data/guides/code/getting_started/config/initializers/locale.rb +0 -9
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -23
- data/guides/code/getting_started/config/routes.rb +0 -7
- data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
- data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -33
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/public/404.html +0 -58
- data/guides/code/getting_started/public/422.html +0 -58
- data/guides/code/getting_started/public/500.html +0 -57
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +0 -5
- data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
- data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
- data/guides/code/getting_started/test/models/comment_test.rb +0 -7
- data/guides/code/getting_started/test/models/post_test.rb +0 -7
- data/guides/code/getting_started/test/test_helper.rb +0 -15
- data/guides/source/kindle/KINDLE.md +0 -26
@@ -3,10 +3,12 @@ A Guide for Upgrading Ruby on Rails
|
|
3
3
|
|
4
4
|
This guide provides steps to be followed when you upgrade your applications to a newer version of Ruby on Rails. These steps are also available in individual release guides.
|
5
5
|
|
6
|
+
--------------------------------------------------------------------------------
|
7
|
+
|
6
8
|
General Advice
|
7
9
|
--------------
|
8
10
|
|
9
|
-
Before attempting to upgrade an existing application, you should be sure you have a good reason to upgrade. You need to balance
|
11
|
+
Before attempting to upgrade an existing application, you should be sure you have a good reason to upgrade. You need to balance several factors: the need for new features, the increasing difficulty of finding support for old code, and your available time and skills, to name a few.
|
10
12
|
|
11
13
|
### Test Coverage
|
12
14
|
|
@@ -22,18 +24,766 @@ Rails generally stays close to the latest released Ruby version when it's releas
|
|
22
24
|
|
23
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.
|
24
26
|
|
25
|
-
|
27
|
+
### The Rake Task
|
28
|
+
|
29
|
+
Rails provides the `rails:update` rake task. After updating the Rails version
|
30
|
+
in the Gemfile, run this rake task.
|
31
|
+
This will help you with the creation of new files and changes of old files in an
|
32
|
+
interactive session.
|
33
|
+
|
34
|
+
```bash
|
35
|
+
$ rake rails:update
|
36
|
+
identical config/boot.rb
|
37
|
+
exist config
|
38
|
+
conflict config/routes.rb
|
39
|
+
Overwrite /myapp/config/routes.rb? (enter "h" for help) [Ynaqdh]
|
40
|
+
force config/routes.rb
|
41
|
+
conflict config/application.rb
|
42
|
+
Overwrite /myapp/config/application.rb? (enter "h" for help) [Ynaqdh]
|
43
|
+
force config/application.rb
|
44
|
+
conflict config/environment.rb
|
45
|
+
...
|
46
|
+
```
|
47
|
+
|
48
|
+
Don't forget to review the difference, to see if there were any unexpected changes.
|
49
|
+
|
50
|
+
Upgrading from Rails 4.1 to Rails 4.2
|
26
51
|
-------------------------------------
|
27
52
|
|
28
|
-
|
53
|
+
### Web Console
|
54
|
+
|
55
|
+
First, add `gem 'web-console', '~> 2.0'` to the `:development` group in your Gemfile and run `bundle install` (it won't have been included when you upgraded Rails). Once it's been installed, you can simply drop a reference to the console helper (i.e., `<%= console %>`) into any view you want to enable it for. A console will also be provided on any error page you view in your development environment.
|
56
|
+
|
57
|
+
### Responders
|
58
|
+
|
59
|
+
`respond_with` and the class-level `respond_to` methods have been extracted to the `responders` gem. To use them, simply add `gem 'responders', '~> 2.0'` to your Gemfile. Calls to `respond_with` and `respond_to` (again, at the class level) will no longer work without having included the `responders` gem in your dependencies:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# app/controllers/users_controller.rb
|
63
|
+
|
64
|
+
class UsersController < ApplicationController
|
65
|
+
respond_to :html, :json
|
66
|
+
|
67
|
+
def show
|
68
|
+
@user = User.find(params[:id])
|
69
|
+
respond_with @user
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
Instance-level `respond_to` is unaffected and does not require the additional gem:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
# app/controllers/users_controller.rb
|
78
|
+
|
79
|
+
class UsersController < ApplicationController
|
80
|
+
def show
|
81
|
+
@user = User.find(params[:id])
|
82
|
+
respond_to do |format|
|
83
|
+
format.html
|
84
|
+
format.json { render json: @user }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
See [#16526](https://github.com/rails/rails/pull/16526) for more details.
|
91
|
+
|
92
|
+
### Error handling in transaction callbacks
|
93
|
+
|
94
|
+
Currently, Active Record suppresses errors raised
|
95
|
+
within `after_rollback` or `after_commit` callbacks and only prints them to
|
96
|
+
the logs. In the next version, these errors will no longer be suppressed.
|
97
|
+
Instead, the errors will propagate normally just like in other Active
|
98
|
+
Record callbacks.
|
99
|
+
|
100
|
+
When you define a `after_rollback` or `after_commit` callback, you
|
101
|
+
will receive a deprecation warning about this upcoming change. When
|
102
|
+
you are ready, you can opt into the new behavior and remove the
|
103
|
+
deprecation warning by adding following configuration to your
|
104
|
+
`config/application.rb`:
|
105
|
+
|
106
|
+
config.active_record.raise_in_transactional_callbacks = true
|
107
|
+
|
108
|
+
See [#14488](https://github.com/rails/rails/pull/14488) and
|
109
|
+
[#16537](https://github.com/rails/rails/pull/16537) for more details.
|
110
|
+
|
111
|
+
### Ordering of test cases
|
112
|
+
|
113
|
+
In Rails 5.0, test cases will be executed in random order by default. In
|
114
|
+
anticipation of this change, Rails 4.2 introduced a new configuration option
|
115
|
+
`active_support.test_order` for explicitly specifying the test ordering. This
|
116
|
+
allows you to either lock down the current behavior by setting the option to
|
117
|
+
`:sorted`, or opt into the future behavior by setting the option to `:random`.
|
118
|
+
|
119
|
+
If you do not specify a value for this option, a deprecation warning will be
|
120
|
+
emitted. To avoid this, add the following line to your test environment:
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
# config/environments/test.rb
|
124
|
+
Rails.application.configure do
|
125
|
+
config.active_support.test_order = :sorted # or `:random` if you prefer
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
129
|
+
### Serialized attributes
|
130
|
+
|
131
|
+
When using a custom coder (e.g. `serialize :metadata, JSON`),
|
132
|
+
assigning `nil` to a serialized attribute will save it to the database
|
133
|
+
as `NULL` instead of passing the `nil` value through the coder (e.g. `"null"`
|
134
|
+
when using the `JSON` coder).
|
135
|
+
|
136
|
+
### Production log level
|
137
|
+
|
138
|
+
In Rails 5, the default log level for the production environment will be changed
|
139
|
+
to `:debug` (from `:info`). To preserve the current default, add the following
|
140
|
+
line to your `production.rb`:
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
# Set to `:info` to match the current default, or set to `:debug` to opt-into
|
144
|
+
# the future default.
|
145
|
+
config.log_level = :info
|
146
|
+
```
|
147
|
+
|
148
|
+
### `after_bundle` in Rails templates
|
149
|
+
|
150
|
+
If you have a Rails template that adds all the files in version control, it
|
151
|
+
fails to add the generated binstubs because it gets executed before Bundler:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
# template.rb
|
155
|
+
generate(:scaffold, "person name:string")
|
156
|
+
route "root to: 'people#index'"
|
157
|
+
rake("db:migrate")
|
158
|
+
|
159
|
+
git :init
|
160
|
+
git add: "."
|
161
|
+
git commit: %Q{ -m 'Initial commit' }
|
162
|
+
```
|
163
|
+
|
164
|
+
You can now wrap the `git` calls in an `after_bundle` block. It will be run
|
165
|
+
after the binstubs have been generated.
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
# template.rb
|
169
|
+
generate(:scaffold, "person name:string")
|
170
|
+
route "root to: 'people#index'"
|
171
|
+
rake("db:migrate")
|
172
|
+
|
173
|
+
after_bundle do
|
174
|
+
git :init
|
175
|
+
git add: "."
|
176
|
+
git commit: %Q{ -m 'Initial commit' }
|
177
|
+
end
|
178
|
+
```
|
179
|
+
|
180
|
+
### Rails HTML Sanitizer
|
181
|
+
|
182
|
+
There's a new choice for sanitizing HTML fragments in your applications. The
|
183
|
+
venerable html-scanner approach is now officially being deprecated in favor of
|
184
|
+
[`Rails HTML Sanitizer`](https://github.com/rails/rails-html-sanitizer).
|
185
|
+
|
186
|
+
This means the methods `sanitize`, `sanitize_css`, `strip_tags` and
|
187
|
+
`strip_links` are backed by a new implementation.
|
188
|
+
|
189
|
+
This new sanitizer uses [Loofah](https://github.com/flavorjones/loofah) internally. Loofah in turn uses Nokogiri, which
|
190
|
+
wraps XML parsers written in both C and Java, so sanitization should be faster
|
191
|
+
no matter which Ruby version you run.
|
192
|
+
|
193
|
+
The new version updates `sanitize`, so it can take a `Loofah::Scrubber` for
|
194
|
+
powerful scrubbing.
|
195
|
+
[See some examples of scrubbers here](https://github.com/flavorjones/loofah#loofahscrubber).
|
196
|
+
|
197
|
+
Two new scrubbers have also been added: `PermitScrubber` and `TargetScrubber`.
|
198
|
+
Read the [gem's readme](https://github.com/rails/rails-html-sanitizer) for more information.
|
199
|
+
|
200
|
+
The documentation for `PermitScrubber` and `TargetScrubber` explains how you
|
201
|
+
can gain complete control over when and how elements should be stripped.
|
202
|
+
|
203
|
+
If your application needs to use the old sanitizer implementation, include `rails-deprecated_sanitizer` in your Gemfile:
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
gem 'rails-deprecated_sanitizer'
|
207
|
+
```
|
208
|
+
|
209
|
+
### Rails DOM Testing
|
210
|
+
|
211
|
+
The [`TagAssertions` module](http://api.rubyonrails.org/classes/ActionDispatch/Assertions/TagAssertions.html) (containing methods such as `assert_tag`), [has been deprecated](https://github.com/rails/rails/blob/6061472b8c310158a2a2e8e9a6b81a1aef6b60fe/actionpack/lib/action_dispatch/testing/assertions/dom.rb) in favor of the `assert_select` methods from the `SelectorAssertions` module, which has been extracted into the [rails-dom-testing gem](https://github.com/rails/rails-dom-testing).
|
212
|
+
|
213
|
+
|
214
|
+
### Masked Authenticity Tokens
|
215
|
+
|
216
|
+
In order to mitigate SSL attacks, `form_authenticity_token` is now masked so that it varies with each request. Thus, tokens are validated by unmasking and then decrypting. As a result, any strategies for verifying requests from non-rails forms that relied on a static session CSRF token have to take this into account.
|
217
|
+
|
218
|
+
### Action Mailer
|
219
|
+
|
220
|
+
Previously, calling a mailer method on a mailer class will result in the
|
221
|
+
corresponding instance method being executed directly. With the introduction of
|
222
|
+
Active Job and `#deliver_later`, this is no longer true. In Rails 4.2, the
|
223
|
+
invocation of the instance methods are deferred until either `deliver_now` or
|
224
|
+
`deliver_later` is called. For example:
|
225
|
+
|
226
|
+
```ruby
|
227
|
+
class Notifier < ActionMailer::Base
|
228
|
+
def notify(user, ...)
|
229
|
+
puts "Called"
|
230
|
+
mail(to: user.email, ...)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
mail = Notifier.notify(user, ...) # Notifier#welcome is not yet called at this point
|
235
|
+
mail = mail.deliver_now # Prints "Called"
|
236
|
+
```
|
237
|
+
|
238
|
+
This should not result in any noticible differnces for most applications.
|
239
|
+
However, if you need some non-mailer methods to be exectuted synchronously, and
|
240
|
+
you were previously relying on the synchronous proxying behavior, you should
|
241
|
+
define them as class methods on the mailer class directly:
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
class Notifier < ActionMailer::Base
|
245
|
+
def self.broadcast_notifications(users, ...)
|
246
|
+
users.each { |user| Notifier.notify(user, ...) }
|
247
|
+
end
|
248
|
+
end
|
249
|
+
```
|
250
|
+
|
251
|
+
### Foreign Key Support
|
252
|
+
|
253
|
+
The migration DSL has been expanded to support foreign key definitions. If
|
254
|
+
you've been using the Foreigner gem, you might want to consider removing it.
|
255
|
+
Note that the foreign key support of Rails is a subset of Foreigner. This means
|
256
|
+
that not every Foreigner definition can be fully replaced by it's Rails
|
257
|
+
migration DSL counterpart.
|
258
|
+
|
259
|
+
The migration procedure is as follows:
|
260
|
+
|
261
|
+
1. remove `gem "foreigner"` from the Gemfile.
|
262
|
+
2. run `bundle install`.
|
263
|
+
3. run `bin/rake db:schema:dump`.
|
264
|
+
4. make sure that `db/schema.rb` contains every foreign key definition with
|
265
|
+
the necessary options.
|
266
|
+
|
267
|
+
Upgrading from Rails 4.0 to Rails 4.1
|
268
|
+
-------------------------------------
|
269
|
+
|
270
|
+
### CSRF protection from remote `<script>` tags
|
271
|
+
|
272
|
+
Or, "whaaat my tests are failing!!!?"
|
273
|
+
|
274
|
+
Cross-site request forgery (CSRF) protection now covers GET requests with
|
275
|
+
JavaScript responses, too. This prevents a third-party site from referencing
|
276
|
+
your JavaScript URL and attempting to run it to extract sensitive data.
|
277
|
+
|
278
|
+
This means that your functional and integration tests that use
|
279
|
+
|
280
|
+
```ruby
|
281
|
+
get :index, format: :js
|
282
|
+
```
|
283
|
+
|
284
|
+
will now trigger CSRF protection. Switch to
|
285
|
+
|
286
|
+
```ruby
|
287
|
+
xhr :get, :index, format: :js
|
288
|
+
```
|
289
|
+
|
290
|
+
to explicitly test an `XmlHttpRequest`.
|
291
|
+
|
292
|
+
If you really mean to load JavaScript from remote `<script>` tags, skip CSRF
|
293
|
+
protection on that action.
|
294
|
+
|
295
|
+
### Spring
|
296
|
+
|
297
|
+
If you want to use Spring as your application preloader you need to:
|
298
|
+
|
299
|
+
1. Add `gem 'spring', group: :development` to your `Gemfile`.
|
300
|
+
2. Install spring using `bundle install`.
|
301
|
+
3. Springify your binstubs with `bundle exec spring binstub --all`.
|
302
|
+
|
303
|
+
NOTE: User defined rake tasks will run in the `development` environment by
|
304
|
+
default. If you want them to run in other environments consult the
|
305
|
+
[Spring README](https://github.com/rails/spring#rake).
|
306
|
+
|
307
|
+
### `config/secrets.yml`
|
308
|
+
|
309
|
+
If you want to use the new `secrets.yml` convention to store your application's
|
310
|
+
secrets, you need to:
|
311
|
+
|
312
|
+
1. Create a `secrets.yml` file in your `config` folder with the following content:
|
313
|
+
|
314
|
+
```yaml
|
315
|
+
development:
|
316
|
+
secret_key_base:
|
317
|
+
|
318
|
+
test:
|
319
|
+
secret_key_base:
|
320
|
+
|
321
|
+
production:
|
322
|
+
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
|
323
|
+
```
|
324
|
+
|
325
|
+
2. Use your existing `secret_key_base` from the `secret_token.rb` initializer to
|
326
|
+
set the SECRET_KEY_BASE environment variable for whichever users running the
|
327
|
+
Rails application in production mode. Alternatively, you can simply copy the existing
|
328
|
+
`secret_key_base` from the `secret_token.rb` initializer to `secrets.yml`
|
329
|
+
under the `production` section, replacing '<%= ENV["SECRET_KEY_BASE"] %>'.
|
330
|
+
|
331
|
+
3. Remove the `secret_token.rb` initializer.
|
332
|
+
|
333
|
+
4. Use `rake secret` to generate new keys for the `development` and `test` sections.
|
334
|
+
|
335
|
+
5. Restart your server.
|
336
|
+
|
337
|
+
### Changes to test helper
|
338
|
+
|
339
|
+
If your test helper contains a call to
|
340
|
+
`ActiveRecord::Migration.check_pending!` this can be removed. The check
|
341
|
+
is now done automatically when you `require 'rails/test_help'`, although
|
342
|
+
leaving this line in your helper is not harmful in any way.
|
343
|
+
|
344
|
+
### Cookies serializer
|
345
|
+
|
346
|
+
Applications created before Rails 4.1 uses `Marshal` to serialize cookie values into
|
347
|
+
the signed and encrypted cookie jars. If you want to use the new `JSON`-based format
|
348
|
+
in your application, you can add an initializer file with the following content:
|
349
|
+
|
350
|
+
```ruby
|
351
|
+
Rails.application.config.action_dispatch.cookies_serializer = :hybrid
|
352
|
+
```
|
353
|
+
|
354
|
+
This would transparently migrate your existing `Marshal`-serialized cookies into the
|
355
|
+
new `JSON`-based format.
|
356
|
+
|
357
|
+
When using the `:json` or `:hybrid` serializer, you should beware that not all
|
358
|
+
Ruby objects can be serialized as JSON. For example, `Date` and `Time` objects
|
359
|
+
will be serialized as strings, and `Hash`es will have their keys stringified.
|
360
|
+
|
361
|
+
```ruby
|
362
|
+
class CookiesController < ApplicationController
|
363
|
+
def set_cookie
|
364
|
+
cookies.encrypted[:expiration_date] = Date.tomorrow # => Thu, 20 Mar 2014
|
365
|
+
redirect_to action: 'read_cookie'
|
366
|
+
end
|
367
|
+
|
368
|
+
def read_cookie
|
369
|
+
cookies.encrypted[:expiration_date] # => "2014-03-20"
|
370
|
+
end
|
371
|
+
end
|
372
|
+
```
|
373
|
+
|
374
|
+
It's advisable that you only store simple data (strings and numbers) in cookies.
|
375
|
+
If you have to store complex objects, you would need to handle the conversion
|
376
|
+
manually when reading the values on subsequent requests.
|
377
|
+
|
378
|
+
If you use the cookie session store, this would apply to the `session` and
|
379
|
+
`flash` hash as well.
|
380
|
+
|
381
|
+
### Flash structure changes
|
382
|
+
|
383
|
+
Flash message keys are
|
384
|
+
[normalized to strings](https://github.com/rails/rails/commit/a668beffd64106a1e1fedb71cc25eaaa11baf0c1). They
|
385
|
+
can still be accessed using either symbols or strings. Looping through the flash
|
386
|
+
will always yield string keys:
|
387
|
+
|
388
|
+
```ruby
|
389
|
+
flash["string"] = "a string"
|
390
|
+
flash[:symbol] = "a symbol"
|
391
|
+
|
392
|
+
# Rails < 4.1
|
393
|
+
flash.keys # => ["string", :symbol]
|
394
|
+
|
395
|
+
# Rails >= 4.1
|
396
|
+
flash.keys # => ["string", "symbol"]
|
397
|
+
```
|
398
|
+
|
399
|
+
Make sure you are comparing Flash message keys against strings.
|
400
|
+
|
401
|
+
### Changes in JSON handling
|
402
|
+
|
403
|
+
There are a few major changes related to JSON handling in Rails 4.1.
|
404
|
+
|
405
|
+
#### MultiJSON removal
|
406
|
+
|
407
|
+
MultiJSON has reached its [end-of-life](https://github.com/rails/rails/pull/10576)
|
408
|
+
and has been removed from Rails.
|
409
|
+
|
410
|
+
If your application currently depend on MultiJSON directly, you have a few options:
|
411
|
+
|
412
|
+
1. Add 'multi_json' to your Gemfile. Note that this might cease to work in the future
|
413
|
+
|
414
|
+
2. Migrate away from MultiJSON by using `obj.to_json`, and `JSON.parse(str)` instead.
|
415
|
+
|
416
|
+
WARNING: Do not simply replace `MultiJson.dump` and `MultiJson.load` with
|
417
|
+
`JSON.dump` and `JSON.load`. These JSON gem APIs are meant for serializing and
|
418
|
+
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).
|
419
|
+
|
420
|
+
#### JSON gem compatibility
|
421
|
+
|
422
|
+
Historically, Rails had some compatibility issues with the JSON gem. Using
|
423
|
+
`JSON.generate` and `JSON.dump` inside a Rails application could produce
|
424
|
+
unexpected errors.
|
425
|
+
|
426
|
+
Rails 4.1 fixed these issues by isolating its own encoder from the JSON gem. The
|
427
|
+
JSON gem APIs will function as normal, but they will not have access to any
|
428
|
+
Rails-specific features. For example:
|
429
|
+
|
430
|
+
```ruby
|
431
|
+
class FooBar
|
432
|
+
def as_json(options = nil)
|
433
|
+
{ foo: 'bar' }
|
434
|
+
end
|
435
|
+
end
|
436
|
+
|
437
|
+
>> FooBar.new.to_json # => "{\"foo\":\"bar\"}"
|
438
|
+
>> JSON.generate(FooBar.new, quirks_mode: true) # => "\"#<FooBar:0x007fa80a481610>\""
|
439
|
+
```
|
440
|
+
|
441
|
+
#### New JSON encoder
|
442
|
+
|
443
|
+
The JSON encoder in Rails 4.1 has been rewritten to take advantage of the JSON
|
444
|
+
gem. For most applications, this should be a transparent change. However, as
|
445
|
+
part of the rewrite, the following features have been removed from the encoder:
|
446
|
+
|
447
|
+
1. Circular data structure detection
|
448
|
+
2. Support for the `encode_json` hook
|
449
|
+
3. Option to encode `BigDecimal` objects as numbers instead of strings
|
450
|
+
|
451
|
+
If your application depends on one of these features, you can get them back by
|
452
|
+
adding the [`activesupport-json_encoder`](https://github.com/rails/activesupport-json_encoder)
|
453
|
+
gem to your Gemfile.
|
454
|
+
|
455
|
+
#### JSON representation of Time objects
|
456
|
+
|
457
|
+
`#as_json` for objects with time component (`Time`, `DateTime`, `ActiveSupport::TimeWithZone`)
|
458
|
+
now returns millisecond precision by default. If you need to keep old behavior with no millisecond
|
459
|
+
precision, set the following in an initializer:
|
460
|
+
|
461
|
+
```
|
462
|
+
ActiveSupport::JSON::Encoding.time_precision = 0
|
463
|
+
```
|
464
|
+
|
465
|
+
### Usage of `return` within inline callback blocks
|
466
|
+
|
467
|
+
Previously, Rails allowed inline callback blocks to use `return` this way:
|
468
|
+
|
469
|
+
```ruby
|
470
|
+
class ReadOnlyModel < ActiveRecord::Base
|
471
|
+
before_save { return false } # BAD
|
472
|
+
end
|
473
|
+
```
|
474
|
+
|
475
|
+
This behavior was never intentionally supported. Due to a change in the internals
|
476
|
+
of `ActiveSupport::Callbacks`, this is no longer allowed in Rails 4.1. Using a
|
477
|
+
`return` statement in an inline callback block causes a `LocalJumpError` to
|
478
|
+
be raised when the callback is executed.
|
479
|
+
|
480
|
+
Inline callback blocks using `return` can be refactored to evaluate to the
|
481
|
+
returned value:
|
482
|
+
|
483
|
+
```ruby
|
484
|
+
class ReadOnlyModel < ActiveRecord::Base
|
485
|
+
before_save { false } # GOOD
|
486
|
+
end
|
487
|
+
```
|
488
|
+
|
489
|
+
Alternatively, if `return` is preferred it is recommended to explicitly define
|
490
|
+
a method:
|
491
|
+
|
492
|
+
```ruby
|
493
|
+
class ReadOnlyModel < ActiveRecord::Base
|
494
|
+
before_save :before_save_callback # GOOD
|
495
|
+
|
496
|
+
private
|
497
|
+
def before_save_callback
|
498
|
+
return false
|
499
|
+
end
|
500
|
+
end
|
501
|
+
```
|
502
|
+
|
503
|
+
This change applies to most places in Rails where callbacks are used, including
|
504
|
+
Active Record and Active Model callbacks, as well as filters in Action
|
505
|
+
Controller (e.g. `before_action`).
|
506
|
+
|
507
|
+
See [this pull request](https://github.com/rails/rails/pull/13271) for more
|
508
|
+
details.
|
509
|
+
|
510
|
+
### Methods defined in Active Record fixtures
|
511
|
+
|
512
|
+
Rails 4.1 evaluates each fixture's ERB in a separate context, so helper methods
|
513
|
+
defined in a fixture will not be available in other fixtures.
|
514
|
+
|
515
|
+
Helper methods that are used in multiple fixtures should be defined on modules
|
516
|
+
included in the newly introduced `ActiveRecord::FixtureSet.context_class`, in
|
517
|
+
`test_helper.rb`.
|
518
|
+
|
519
|
+
```ruby
|
520
|
+
module FixtureFileHelpers
|
521
|
+
def file_sha(path)
|
522
|
+
Digest::SHA2.hexdigest(File.read(Rails.root.join('test/fixtures', path)))
|
523
|
+
end
|
524
|
+
end
|
525
|
+
ActiveRecord::FixtureSet.context_class.send :include, FixtureFileHelpers
|
526
|
+
```
|
527
|
+
|
528
|
+
### I18n enforcing available locales
|
529
|
+
|
530
|
+
Rails 4.1 now defaults the I18n option `enforce_available_locales` to `true`. This
|
531
|
+
means that it will make sure that all locales passed to it must be declared in
|
532
|
+
the `available_locales` list.
|
533
|
+
|
534
|
+
To disable it (and allow I18n to accept *any* locale option) add the following
|
535
|
+
configuration to your application:
|
536
|
+
|
537
|
+
```ruby
|
538
|
+
config.i18n.enforce_available_locales = false
|
539
|
+
```
|
540
|
+
|
541
|
+
Note that this option was added as a security measure, to ensure user input
|
542
|
+
cannot be used as locale information unless it is previously known. Therefore,
|
543
|
+
it's recommended not to disable this option unless you have a strong reason for
|
544
|
+
doing so.
|
545
|
+
|
546
|
+
### Mutator methods called on Relation
|
547
|
+
|
548
|
+
`Relation` no longer has mutator methods like `#map!` and `#delete_if`. Convert
|
549
|
+
to an `Array` by calling `#to_a` before using these methods.
|
550
|
+
|
551
|
+
It intends to prevent odd bugs and confusion in code that call mutator
|
552
|
+
methods directly on the `Relation`.
|
553
|
+
|
554
|
+
```ruby
|
555
|
+
# Instead of this
|
556
|
+
Author.where(name: 'Hank Moody').compact!
|
557
|
+
|
558
|
+
# Now you have to do this
|
559
|
+
authors = Author.where(name: 'Hank Moody').to_a
|
560
|
+
authors.compact!
|
561
|
+
```
|
562
|
+
|
563
|
+
### Changes on Default Scopes
|
564
|
+
|
565
|
+
Default scopes are no longer overridden by chained conditions.
|
566
|
+
|
567
|
+
In previous versions when you defined a `default_scope` in a model
|
568
|
+
it was overridden by chained conditions in the same field. Now it
|
569
|
+
is merged like any other scope.
|
570
|
+
|
571
|
+
Before:
|
572
|
+
|
573
|
+
```ruby
|
574
|
+
class User < ActiveRecord::Base
|
575
|
+
default_scope { where state: 'pending' }
|
576
|
+
scope :active, -> { where state: 'active' }
|
577
|
+
scope :inactive, -> { where state: 'inactive' }
|
578
|
+
end
|
579
|
+
|
580
|
+
User.all
|
581
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
|
582
|
+
|
583
|
+
User.active
|
584
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'active'
|
585
|
+
|
586
|
+
User.where(state: 'inactive')
|
587
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'inactive'
|
588
|
+
```
|
589
|
+
|
590
|
+
After:
|
591
|
+
|
592
|
+
```ruby
|
593
|
+
class User < ActiveRecord::Base
|
594
|
+
default_scope { where state: 'pending' }
|
595
|
+
scope :active, -> { where state: 'active' }
|
596
|
+
scope :inactive, -> { where state: 'inactive' }
|
597
|
+
end
|
598
|
+
|
599
|
+
User.all
|
600
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
|
601
|
+
|
602
|
+
User.active
|
603
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending' AND "users"."state" = 'active'
|
604
|
+
|
605
|
+
User.where(state: 'inactive')
|
606
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending' AND "users"."state" = 'inactive'
|
607
|
+
```
|
608
|
+
|
609
|
+
To get the previous behavior it is needed to explicitly remove the
|
610
|
+
`default_scope` condition using `unscoped`, `unscope`, `rewhere` or
|
611
|
+
`except`.
|
612
|
+
|
613
|
+
```ruby
|
614
|
+
class User < ActiveRecord::Base
|
615
|
+
default_scope { where state: 'pending' }
|
616
|
+
scope :active, -> { unscope(where: :state).where(state: 'active') }
|
617
|
+
scope :inactive, -> { rewhere state: 'inactive' }
|
618
|
+
end
|
619
|
+
|
620
|
+
User.all
|
621
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
|
622
|
+
|
623
|
+
User.active
|
624
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'active'
|
625
|
+
|
626
|
+
User.inactive
|
627
|
+
# SELECT "users".* FROM "users" WHERE "users"."state" = 'inactive'
|
628
|
+
```
|
629
|
+
|
630
|
+
### Rendering content from string
|
631
|
+
|
632
|
+
Rails 4.1 introduces `:plain`, `:html`, and `:body` options to `render`. Those
|
633
|
+
options are now the preferred way to render string-based content, as it allows
|
634
|
+
you to specify which content type you want the response sent as.
|
635
|
+
|
636
|
+
* `render :plain` will set the content type to `text/plain`
|
637
|
+
* `render :html` will set the content type to `text/html`
|
638
|
+
* `render :body` will *not* set the content type header.
|
639
|
+
|
640
|
+
From the security standpoint, if you don't expect to have any markup in your
|
641
|
+
response body, you should be using `render :plain` as most browsers will escape
|
642
|
+
unsafe content in the response for you.
|
643
|
+
|
644
|
+
We will be deprecating the use of `render :text` in a future version. So please
|
645
|
+
start using the more precise `:plain`, `:html`, and `:body` options instead.
|
646
|
+
Using `render :text` may pose a security risk, as the content is sent as
|
647
|
+
`text/html`.
|
648
|
+
|
649
|
+
### PostgreSQL json and hstore datatypes
|
650
|
+
|
651
|
+
Rails 4.1 will map `json` and `hstore` columns to a string-keyed Ruby `Hash`.
|
652
|
+
In earlier versions, a `HashWithIndifferentAccess` was used. This means that
|
653
|
+
symbol access is no longer supported. This is also the case for
|
654
|
+
`store_accessors` based on top of `json` or `hstore` columns. Make sure to use
|
655
|
+
string keys consistently.
|
656
|
+
|
657
|
+
### Explicit block use for `ActiveSupport::Callbacks`
|
658
|
+
|
659
|
+
Rails 4.1 now expects an explicit block to be passed when calling
|
660
|
+
`ActiveSupport::Callbacks.set_callback`. This change stems from
|
661
|
+
`ActiveSupport::Callbacks` being largely rewritten for the 4.1 release.
|
662
|
+
|
663
|
+
```ruby
|
664
|
+
# Previously in Rails 4.0
|
665
|
+
set_callback :save, :around, ->(r, &block) { stuff; result = block.call; stuff }
|
666
|
+
|
667
|
+
# Now in Rails 4.1
|
668
|
+
set_callback :save, :around, ->(r, block) { stuff; result = block.call; stuff }
|
669
|
+
```
|
670
|
+
|
671
|
+
Upgrading from Rails 3.2 to Rails 4.0
|
672
|
+
-------------------------------------
|
29
673
|
|
30
674
|
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.
|
31
675
|
|
32
676
|
The following changes are meant for upgrading your application to Rails 4.0.
|
33
677
|
|
678
|
+
### HTTP PATCH
|
679
|
+
|
680
|
+
Rails 4 now uses `PATCH` as the primary HTTP verb for updates when a RESTful
|
681
|
+
resource is declared in `config/routes.rb`. The `update` action is still used,
|
682
|
+
and `PUT` requests will continue to be routed to the `update` action as well.
|
683
|
+
So, if you're using only the standard RESTful routes, no changes need to be made:
|
684
|
+
|
685
|
+
```ruby
|
686
|
+
resources :users
|
687
|
+
```
|
688
|
+
|
689
|
+
```erb
|
690
|
+
<%= form_for @user do |f| %>
|
691
|
+
```
|
692
|
+
|
693
|
+
```ruby
|
694
|
+
class UsersController < ApplicationController
|
695
|
+
def update
|
696
|
+
# No change needed; PATCH will be preferred, and PUT will still work.
|
697
|
+
end
|
698
|
+
end
|
699
|
+
```
|
700
|
+
|
701
|
+
However, you will need to make a change if you are using `form_for` to update
|
702
|
+
a resource in conjunction with a custom route using the `PUT` HTTP method:
|
703
|
+
|
704
|
+
```ruby
|
705
|
+
resources :users, do
|
706
|
+
put :update_name, on: :member
|
707
|
+
end
|
708
|
+
```
|
709
|
+
|
710
|
+
```erb
|
711
|
+
<%= form_for [ :update_name, @user ] do |f| %>
|
712
|
+
```
|
713
|
+
|
714
|
+
```ruby
|
715
|
+
class UsersController < ApplicationController
|
716
|
+
def update_name
|
717
|
+
# Change needed; form_for will try to use a non-existent PATCH route.
|
718
|
+
end
|
719
|
+
end
|
720
|
+
```
|
721
|
+
|
722
|
+
If the action is not being used in a public API and you are free to change the
|
723
|
+
HTTP method, you can update your route to use `patch` instead of `put`:
|
724
|
+
|
725
|
+
`PUT` requests to `/users/:id` in Rails 4 get routed to `update` as they are
|
726
|
+
today. So, if you have an API that gets real PUT requests it is going to work.
|
727
|
+
The router also routes `PATCH` requests to `/users/:id` to the `update` action.
|
728
|
+
|
729
|
+
```ruby
|
730
|
+
resources :users do
|
731
|
+
patch :update_name, on: :member
|
732
|
+
end
|
733
|
+
```
|
734
|
+
|
735
|
+
If the action is being used in a public API and you can't change to HTTP method
|
736
|
+
being used, you can update your form to use the `PUT` method instead:
|
737
|
+
|
738
|
+
```erb
|
739
|
+
<%= form_for [ :update_name, @user ], method: :put do |f| %>
|
740
|
+
```
|
741
|
+
|
742
|
+
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/)
|
743
|
+
on the Rails blog.
|
744
|
+
|
745
|
+
#### A note about media types
|
746
|
+
|
747
|
+
The errata for the `PATCH` verb [specifies that a 'diff' media type should be
|
748
|
+
used with `PATCH`](http://www.rfc-editor.org/errata_search.php?rfc=5789). One
|
749
|
+
such format is [JSON Patch](http://tools.ietf.org/html/rfc6902). While Rails
|
750
|
+
does not support JSON Patch natively, it's easy enough to add support:
|
751
|
+
|
752
|
+
```
|
753
|
+
# in your controller
|
754
|
+
def update
|
755
|
+
respond_to do |format|
|
756
|
+
format.json do
|
757
|
+
# perform a partial update
|
758
|
+
@article.update params[:article]
|
759
|
+
end
|
760
|
+
|
761
|
+
format.json_patch do
|
762
|
+
# perform sophisticated change
|
763
|
+
end
|
764
|
+
end
|
765
|
+
end
|
766
|
+
|
767
|
+
# In config/initializers/json_patch.rb:
|
768
|
+
Mime::Type.register 'application/json-patch+json', :json_patch
|
769
|
+
```
|
770
|
+
|
771
|
+
As JSON Patch was only recently made into an RFC, there aren't a lot of great
|
772
|
+
Ruby libraries yet. Aaron Patterson's
|
773
|
+
[hana](https://github.com/tenderlove/hana) is one such gem, but doesn't have
|
774
|
+
full support for the last few changes in the specification.
|
775
|
+
|
34
776
|
### Gemfile
|
35
777
|
|
36
|
-
Rails 4.0 removed the `assets` group from Gemfile. You'd need to remove that
|
778
|
+
Rails 4.0 removed the `assets` group from Gemfile. You'd need to remove that
|
779
|
+
line from your Gemfile when upgrading. You should also update your application
|
780
|
+
file (in `config/application.rb`):
|
781
|
+
|
782
|
+
```ruby
|
783
|
+
# Require the gems listed in Gemfile, including any gems
|
784
|
+
# you've limited to :test, :development, or :production.
|
785
|
+
Bundler.require(*Rails.groups)
|
786
|
+
```
|
37
787
|
|
38
788
|
### vendor/plugins
|
39
789
|
|
@@ -43,13 +793,19 @@ Rails 4.0 no longer supports loading plugins from `vendor/plugins`. You must rep
|
|
43
793
|
|
44
794
|
* Rails 4.0 has removed the identity map from Active Record, due to [some inconsistencies with associations](https://github.com/rails/rails/commit/302c912bf6bcd0fa200d964ec2dc4a44abe328a6). If you have manually enabled it in your application, you will have to remove the following config that has no effect anymore: `config.active_record.identity_map`.
|
45
795
|
|
46
|
-
* The `delete` method in collection associations can now receive `
|
796
|
+
* The `delete` method in collection associations can now receive `Integer` or `String` arguments as record ids, besides records, pretty much like the `destroy` method does. Previously it raised `ActiveRecord::AssociationTypeMismatch` for such arguments. From Rails 4.0 on `delete` automatically tries to find the records matching the given ids before deleting them.
|
47
797
|
|
48
|
-
* Rails 4.0
|
798
|
+
* In Rails 4.0 when a column or a table is renamed the related indexes are also renamed. If you have migrations which rename the indexes, they are no longer needed.
|
49
799
|
|
50
800
|
* Rails 4.0 has changed `serialized_attributes` and `attr_readonly` to class methods only. You shouldn't use instance methods since it's now deprecated. You should change them to use class methods, e.g. `self.serialized_attributes` to `self.class.serialized_attributes`.
|
51
801
|
|
52
|
-
*
|
802
|
+
* When using the default coder, assigning `nil` to a serialized attribute will save it
|
803
|
+
to the database as `NULL` instead of passing the `nil` value through YAML (`"--- \n...\n"`).
|
804
|
+
|
805
|
+
* Rails 4.0 has removed `attr_accessible` and `attr_protected` feature in favor of Strong Parameters. You can use the [Protected Attributes gem](https://github.com/rails/protected_attributes) for a smooth upgrade path.
|
806
|
+
|
807
|
+
* If you are not using Protected Attributes, you can remove any options related to
|
808
|
+
this gem such as `whitelist_attributes` or `mass_assignment_sanitizer` options.
|
53
809
|
|
54
810
|
* Rails 4.0 requires that scopes use a callable object such as a Proc or lambda:
|
55
811
|
|
@@ -61,8 +817,41 @@ Rails 4.0 no longer supports loading plugins from `vendor/plugins`. You must rep
|
|
61
817
|
```
|
62
818
|
|
63
819
|
* Rails 4.0 has deprecated `ActiveRecord::Fixtures` in favor of `ActiveRecord::FixtureSet`.
|
820
|
+
|
64
821
|
* Rails 4.0 has deprecated `ActiveRecord::TestCase` in favor of `ActiveSupport::TestCase`.
|
65
822
|
|
823
|
+
* Rails 4.0 has deprecated the old-style hash based finder API. This means that
|
824
|
+
methods which previously accepted "finder options" no longer do. For example, `Book.find(:all, conditions: { name: '1984' })` has been deprecated in favor of `Book.where(name: '1984')`
|
825
|
+
|
826
|
+
* All dynamic methods except for `find_by_...` and `find_by_...!` are deprecated.
|
827
|
+
Here's how you can handle the changes:
|
828
|
+
|
829
|
+
* `find_all_by_...` becomes `where(...)`.
|
830
|
+
* `find_last_by_...` becomes `where(...).last`.
|
831
|
+
* `scoped_by_...` becomes `where(...)`.
|
832
|
+
* `find_or_initialize_by_...` becomes `find_or_initialize_by(...)`.
|
833
|
+
* `find_or_create_by_...` becomes `find_or_create_by(...)`.
|
834
|
+
|
835
|
+
* Note that `where(...)` returns a relation, not an array like the old finders. If you require an `Array`, use `where(...).to_a`.
|
836
|
+
|
837
|
+
* These equivalent methods may not execute the same SQL as the previous implementation.
|
838
|
+
|
839
|
+
* To re-enable the old finders, you can use the [activerecord-deprecated_finders gem](https://github.com/rails/activerecord-deprecated_finders).
|
840
|
+
|
841
|
+
* Rails 4.0 has changed to default join table for `has_and_belongs_to_many` relations to strip the common prefix off the second table name. Any existing `has_and_belongs_to_many` relationship between models with a common prefix must be specified with the `join_table` option. For example:
|
842
|
+
|
843
|
+
```ruby
|
844
|
+
CatalogCategory < ActiveRecord::Base
|
845
|
+
has_and_belongs_to_many :catalog_products, join_table: 'catalog_categories_catalog_products'
|
846
|
+
end
|
847
|
+
|
848
|
+
CatalogProduct < ActiveRecord::Base
|
849
|
+
has_and_belongs_to_many :catalog_categories, join_table: 'catalog_categories_catalog_products'
|
850
|
+
end
|
851
|
+
```
|
852
|
+
|
853
|
+
* Note that the the prefix takes scopes into account as well, so relations between `Catalog::Category` and `Catalog::Product` or `Catalog::Category` and `CatalogProduct` need to be updated similarly.
|
854
|
+
|
66
855
|
### Active Resource
|
67
856
|
|
68
857
|
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.
|
@@ -71,7 +860,7 @@ Rails 4.0 extracted Active Resource to its own gem. If you still need the featur
|
|
71
860
|
|
72
861
|
* Rails 4.0 has changed how errors attach with the `ActiveModel::Validations::ConfirmationValidator`. Now when confirmation validations fail, the error will be attached to `:#{attribute}_confirmation` instead of `attribute`.
|
73
862
|
|
74
|
-
* Rails 4.0 has changed `ActiveModel::Serializers::JSON.include_root_in_json` default value to `false`. Now, Active Model Serializers and Active Record objects have the same default
|
863
|
+
* Rails 4.0 has changed `ActiveModel::Serializers::JSON.include_root_in_json` default value to `false`. Now, Active Model Serializers and Active Record objects have the same default behavior. This means that you can comment or remove the following option in the `config/initializers/wrap_parameters.rb` file:
|
75
864
|
|
76
865
|
```ruby
|
77
866
|
# Disable root element in JSON by default.
|
@@ -96,16 +885,6 @@ If you are relying on the ability for external applications or Javascript to be
|
|
96
885
|
|
97
886
|
* Rails 4.0 encrypts the contents of cookie-based sessions if `secret_key_base` has been set. Rails 3.x signed, but did not encrypt, the contents of cookie-based session. Signed cookies are "secure" in that they are verified to have been generated by your app and are tamper-proof. However, the contents can be viewed by end users, and encrypting the contents eliminates this caveat/concern without a significant performance penalty.
|
98
887
|
|
99
|
-
As described above, existing signed cookies generated with Rails 3.x will be transparently upgraded if you leave your existing `secret_token` in place and add the new `secret_key_base`.
|
100
|
-
|
101
|
-
```ruby
|
102
|
-
# config/initializers/secret_token.rb
|
103
|
-
Myapp::Application.config.secret_token = 'existing secret token'
|
104
|
-
Myapp::Application.config.secret_key_base = 'new secret key base'
|
105
|
-
```
|
106
|
-
|
107
|
-
The same caveats apply here, too. You should wait to set `secret_key_base` until you have 100% of your userbase on Rails 4.x and are reasonably sure you will not need to rollback to Rails 3.x. You should also take care to make sure you are not relying on the ability to decode signed cookies generated by your app in external applications or Javascript before upgrading.
|
108
|
-
|
109
888
|
Please read [Pull Request #9978](https://github.com/rails/rails/pull/9978) for details on the move to encrypted session cookies.
|
110
889
|
|
111
890
|
* Rails 4.0 removed the `ActionController::Base.asset_path` option. Use the assets pipeline feature.
|
@@ -116,10 +895,17 @@ Please read [Pull Request #9978](https://github.com/rails/rails/pull/9978) for d
|
|
116
895
|
|
117
896
|
* Rails 4.0 has removed the XML parameters parser. You will need to add the `actionpack-xml_parser` gem if you require this feature.
|
118
897
|
|
898
|
+
* Rails 4.0 changes the default `layout` lookup set using symbols or procs that return nil. To get the "no layout" behavior, return false instead of nil.
|
899
|
+
|
119
900
|
* Rails 4.0 changes the default memcached client from `memcache-client` to `dalli`. To upgrade, simply add `gem 'dalli'` to your `Gemfile`.
|
120
901
|
|
121
902
|
* Rails 4.0 deprecates the `dom_id` and `dom_class` methods in controllers (they are fine in views). You will need to include the `ActionView::RecordIdentifier` module in controllers requiring this feature.
|
122
903
|
|
904
|
+
* Rails 4.0 deprecates the `:confirm` option for the `link_to` helper. You should
|
905
|
+
instead rely on a data attribute (e.g. `data: { confirm: 'Are you sure?' }`).
|
906
|
+
This deprecation also concerns the helpers based on this one (such as `link_to_if`
|
907
|
+
or `link_to_unless`).
|
908
|
+
|
123
909
|
* Rails 4.0 changed how `assert_generates`, `assert_recognizes`, and `assert_routing` work. Now all these assertions raise `Assertion` instead of `ActionController::RoutingError`.
|
124
910
|
|
125
911
|
* Rails 4.0 raises an `ArgumentError` if clashing named routes are defined. This can be triggered by explicitly defined named routes or by the `resources` method. Here are two examples that clash with routes named `example_path`:
|
@@ -155,13 +941,13 @@ get 'こんにちは', controller: 'welcome', action: 'index'
|
|
155
941
|
|
156
942
|
```ruby
|
157
943
|
# Rails 3.x
|
158
|
-
match
|
944
|
+
match '/' => 'root#index'
|
159
945
|
|
160
946
|
# becomes
|
161
|
-
match
|
947
|
+
match '/' => 'root#index', via: :get
|
162
948
|
|
163
949
|
# or
|
164
|
-
get
|
950
|
+
get '/' => 'root#index'
|
165
951
|
```
|
166
952
|
|
167
953
|
* 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`.
|
@@ -202,33 +988,40 @@ The order in which helpers from more than one directory are loaded has changed i
|
|
202
988
|
|
203
989
|
### Active Record Observer and Action Controller Sweeper
|
204
990
|
|
205
|
-
|
991
|
+
`ActiveRecord::Observer` and `ActionController::Caching::Sweeper` have been extracted to the `rails-observers` gem. You will need to add the `rails-observers` gem if you require these features.
|
206
992
|
|
207
993
|
### sprockets-rails
|
208
994
|
|
209
|
-
* `assets:precompile:primary`
|
995
|
+
* `assets:precompile:primary` and `assets:precompile:all` have been removed. Use `assets:precompile` instead.
|
996
|
+
* The `config.assets.compress` option should be changed to `config.assets.js_compressor` like so for instance:
|
997
|
+
|
998
|
+
```ruby
|
999
|
+
config.assets.js_compressor = :uglifier
|
1000
|
+
```
|
210
1001
|
|
211
1002
|
### sass-rails
|
212
1003
|
|
213
|
-
* `
|
1004
|
+
* `asset-url` with two arguments is deprecated. For example: `asset-url("rails.png", image)` becomes `asset-url("rails.png")`.
|
214
1005
|
|
215
1006
|
Upgrading from Rails 3.1 to Rails 3.2
|
216
1007
|
-------------------------------------
|
217
1008
|
|
218
|
-
If your application is currently on any version of Rails older than 3.1.x, you
|
1009
|
+
If your application is currently on any version of Rails older than 3.1.x, you
|
1010
|
+
should upgrade to Rails 3.1 before attempting an update to Rails 3.2.
|
219
1011
|
|
220
|
-
The following changes are meant for upgrading your application to
|
1012
|
+
The following changes are meant for upgrading your application to the latest
|
1013
|
+
3.2.x version of Rails.
|
221
1014
|
|
222
1015
|
### Gemfile
|
223
1016
|
|
224
1017
|
Make the following changes to your `Gemfile`.
|
225
1018
|
|
226
1019
|
```ruby
|
227
|
-
gem 'rails', '
|
1020
|
+
gem 'rails', '3.2.18'
|
228
1021
|
|
229
1022
|
group :assets do
|
230
|
-
gem 'sass-rails', '~> 3.2.
|
231
|
-
gem 'coffee-rails', '~> 3.2.
|
1023
|
+
gem 'sass-rails', '~> 3.2.6'
|
1024
|
+
gem 'coffee-rails', '~> 3.2.2'
|
232
1025
|
gem 'uglifier', '>= 1.0.3'
|
233
1026
|
end
|
234
1027
|
```
|
@@ -259,26 +1052,30 @@ config.active_record.mass_assignment_sanitizer = :strict
|
|
259
1052
|
|
260
1053
|
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`.
|
261
1054
|
|
1055
|
+
### Active Record
|
1056
|
+
|
1057
|
+
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.
|
1058
|
+
|
262
1059
|
Upgrading from Rails 3.0 to Rails 3.1
|
263
1060
|
-------------------------------------
|
264
1061
|
|
265
1062
|
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.
|
266
1063
|
|
267
|
-
The following changes are meant for upgrading your application to Rails 3.1.
|
1064
|
+
The following changes are meant for upgrading your application to Rails 3.1.12, the last 3.1.x version of Rails.
|
268
1065
|
|
269
1066
|
### Gemfile
|
270
1067
|
|
271
1068
|
Make the following changes to your `Gemfile`.
|
272
1069
|
|
273
1070
|
```ruby
|
274
|
-
gem 'rails', '
|
1071
|
+
gem 'rails', '3.1.12'
|
275
1072
|
gem 'mysql2'
|
276
1073
|
|
277
1074
|
# Needed for the new asset pipeline
|
278
1075
|
group :assets do
|
279
|
-
gem 'sass-rails',
|
280
|
-
gem 'coffee-rails',
|
281
|
-
gem 'uglifier',
|
1076
|
+
gem 'sass-rails', '~> 3.1.7'
|
1077
|
+
gem 'coffee-rails', '~> 3.1.1'
|
1078
|
+
gem 'uglifier', '>= 1.0.3'
|
282
1079
|
end
|
283
1080
|
|
284
1081
|
# jQuery is the default JavaScript library in Rails 3.1
|
@@ -345,8 +1142,8 @@ You can help test performance with these additions to your test environment:
|
|
345
1142
|
|
346
1143
|
```ruby
|
347
1144
|
# Configure static asset server for tests with Cache-Control for performance
|
348
|
-
config.
|
349
|
-
config.static_cache_control =
|
1145
|
+
config.serve_static_files = true
|
1146
|
+
config.static_cache_control = 'public, max-age=3600'
|
350
1147
|
```
|
351
1148
|
|
352
1149
|
### config/initializers/wrap_parameters.rb
|
@@ -381,7 +1178,7 @@ AppName::Application.config.session_store :cookie_store, key: 'SOMETHINGNEW'
|
|
381
1178
|
or
|
382
1179
|
|
383
1180
|
```bash
|
384
|
-
$ rake db:sessions:clear
|
1181
|
+
$ bin/rake db:sessions:clear
|
385
1182
|
```
|
386
1183
|
|
387
1184
|
### Remove :cache and :concat options in asset helpers references in views
|