upgrading_ruby_on_rails.md 42.2 KB
Newer Older
1 2
A Guide for Upgrading Ruby on Rails
===================================
3 4 5

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.

6 7
--------------------------------------------------------------------------------

8 9
General Advice
--------------
10

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.
12

13
### Test Coverage
14 15 16

The best way to be sure that your application still works after upgrading is to have good test coverage before you start the process. If you don't have automated tests that exercise the bulk of your application, you'll need to spend time manually exercising all the parts that have changed. In the case of a Rails upgrade, that will mean every single piece of functionality in the application. Do yourself a favor and make sure your test coverage is good _before_ you start an upgrade.

17
### Ruby Versions
18 19 20

Rails generally stays close to the latest released Ruby version when it's released:

J
Jeremy Kemper 已提交
21 22 23
* Rails 3 and above require Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially. You should upgrade as early as possible.
* Rails 3.2.x is the last branch to support Ruby 1.8.7.
* Rails 4 prefers Ruby 2.0 and requires 1.9.3 or newer.
24

J
Jeremy Kemper 已提交
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

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
### The Rake Task

Rails provides the `rails:update` rake task. After updating the Rails version
in the Gemfile, run this rake task.
This will help you with the creation of new files and changes of old files in a
interactive session.

```bash
$ rake rails:update
   identical  config/boot.rb
       exist  config
    conflict  config/routes.rb
Overwrite /myapp/config/routes.rb? (enter "h" for help) [Ynaqdh]
       force  config/routes.rb
    conflict  config/application.rb
Overwrite /myapp/config/application.rb? (enter "h" for help) [Ynaqdh]
       force  config/application.rb
    conflict  config/environment.rb
...
```

Don't forget to review the difference, to see if there were any unexpected changes.

50 51 52
Upgrading from Rails 4.1 to Rails 4.2
-------------------------------------

53 54
NOTE: This section is a work in progress, please help to improve this by sending
a [pull request](https://github.com/rails/rails/edit/master/guides/source/upgrading_ruby_on_rails.md).
55

56 57
### Web Console

58
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.
59

60
Additionally, you can tell Rails to automatically mount a VT100-compatible console on a predetermined path by setting the appropriate configuration flags in your development config:
61

62 63 64 65 66 67
```ruby
# config/environments/development.rb

config.web_console.automount = true
config.web_console.default_mount_path = '/terminal' # Optional, defaults to /console
```
68 69 70

### Responders

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
`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:

```ruby
# app/controllers/users_controller.rb

class UsersController < ApplicationController
  respond_to :html, :json

  def show
    @user = User.find(params[:id])
    respond_with @user
  end
end
```

Instance-level `respond_to` is unaffected and does not require the additional gem:

```ruby
# app/controllers/users_controller.rb

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    respond_to do |format|
      format.html
      format.json { render json: @user }
    end
  end
end
```

See [#16526](https://github.com/rails/rails/pull/16526) for more details.
103 104 105

### Error handling in transaction callbacks

106 107 108 109 110 111
Currently, Active Record suppresses errors raised
within `after_rollback` or `after_commit` callbacks and only prints them to
the logs. In the next version, these errors will no longer be suppressed.
Instead, the errors will propagate normally just like in other Active
Record callbacks.

G
Godfrey Chan 已提交
112
When you define a `after_rollback` or `after_commit` callback, you
113 114 115 116 117 118 119 120 121
will receive a deprecation warning about this upcoming change. When
you are ready, you can opt into the new behvaior and remove the
deprecation warning by adding following configuration to your
`config/application.rb`:

    config.active_record.raise_in_transactional_callbacks = true

See [#14488](https://github.com/rails/rails/pull/14488) and
[#16537](https://github.com/rails/rails/pull/16537) for more details.
122

123 124 125 126 127
### Ordering of test cases

In Rails 5.0, test cases will be executed in random order by default. In
anticipation of this change, Rails 4.2 introduced a new configuration option
`active_support.test_order` for explicitly specifying the test ordering. This
R
Rafael Mendonça França 已提交
128
allows you to either lock down the current behavior by setting the option to
129 130 131 132 133 134 135 136 137 138 139 140
`:sorted`, or opt into the future behavior by setting the option to `:random`.

If you do not specify a value for this option, a deprecation warning will be
emitted. To avoid this, add the following line to your test environment:

```ruby
# config/environments/test.rb
Rails.application.configure do
  config.active_support.test_order = :sorted # or `:random` if you prefer
end
```

141 142
### Serialized attributes

143 144
When using a custom coder (e.g. `serialize :metadata, JSON`),
assigning `nil` to a serialized attribute will save it to the database
145 146
as `NULL` instead of passing the `nil` value through the coder (e.g. `"null"`
when using the `JSON` coder).
147

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
### `after_bundle` in Rails templates

If you have a Rails template that adds all the files in version control, it
fails to add the generated binstubs because it gets executed before Bundler:

```ruby
# template.rb
generate(:scaffold, "person name:string")
route "root to: 'people#index'"
rake("db:migrate")

git :init
git add: "."
git commit: %Q{ -m 'Initial commit' }
```

You can now wrap the `git` calls in an `after_bundle` block. It will be run
after the binstubs have been generated.

```ruby
# template.rb
generate(:scaffold, "person name:string")
route "root to: 'people#index'"
rake("db:migrate")

after_bundle do
  git :init
  git add: "."
  git commit: %Q{ -m 'Initial commit' }
end
```

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
### Rails Html Sanitizer

There's a new choice for sanitizing HTML fragments in your applications. The
venerable html-scanner approach is now officially being deprecated in favor of
[`Rails Html Sanitizer`](https://github.com/rails/rails-html-sanitizer).

This means the methods `sanitize`, `sanitize_css`, `strip_tags` and
`strip_links` are backed by a new implementation.

This new sanitizer uses [Loofah](https://github.com/flavorjones/loofah) internally. Loofah in turn uses Nokogiri, which
wraps XML parsers written in both C and Java, so sanitization should be faster
no matter which Ruby version you run.

The new version updates `sanitize`, so it can take a `Loofah::Scrubber` for
powerful scrubbing.
[See some examples of scrubbers here](https://github.com/flavorjones/loofah#loofahscrubber).

Two new scrubbers have also been added: `PermitScrubber` and `TargetScrubber`.
Read the [gem's readme](https://github.com/rails/rails-html-sanitizer) for more information.

The documentation for `PermitScrubber` and `TargetScrubber` explains how you
can gain complete control over when and how elements should be stripped.

203 204 205 206 207 208
If your application needs to old behaviour include `rails-deprecated_sanitizer` in your Gemfile:

```ruby
gem 'rails-deprecated_sanitizer'
```

209 210 211 212
### Rails DOM Testing

TODO: Mention https://github.com/rails/rails/commit/4e97d7585a2f4788b9eed98c6cdaf4bb6f2cf5ce

213 214 215
Upgrading from Rails 4.0 to Rails 4.1
-------------------------------------

216 217 218 219 220
### CSRF protection from remote `<script>` tags

Or, "whaaat my tests are failing!!!?"

Cross-site request forgery (CSRF) protection now covers GET requests with
G
Guo Xiang Tan 已提交
221
JavaScript responses, too. This prevents a third-party site from referencing
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
your JavaScript URL and attempting to run it to extract sensitive data.

This means that your functional and integration tests that use

```ruby
get :index, format: :js
```

will now trigger CSRF protection. Switch to

```ruby
xhr :get, :index, format: :js
```

to explicitly test an XmlHttpRequest.

If you really mean to load JavaScript from remote `<script>` tags, skip CSRF
protection on that action.

241 242 243 244
### Spring

If you want to use Spring as your application preloader you need to:

245 246 247
1. Add `gem 'spring', group: :development` to your `Gemfile`.
2. Install spring using `bundle install`.
3. Springify your binstubs with `bundle exec spring binstub --all`.
248 249 250

NOTE: User defined rake tasks will run in the `development` environment by
default. If you want them to run in other environments consult the
251
[Spring README](https://github.com/rails/spring#rake).
252

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
### `config/secrets.yml`

If you want to use the new `secrets.yml` convention to store your application's
secrets, you need to:

1. Create a `secrets.yml` file in your `config` folder with the following content:

    ```yaml
    development:
      secret_key_base:

    test:
      secret_key_base:

    production:
268
      secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
269 270
    ```

271
2. Use your existing `secret_key_base` from the `secret_token.rb` initializer to
G
Guo Xiang Tan 已提交
272
   set the SECRET_KEY_BASE environment variable for whichever users running the
G
Guo Xiang Tan 已提交
273
   Rails application in production mode. Alternatively, you can simply copy the existing
274
   `secret_key_base` from the `secret_token.rb` initializer to `secrets.yml`
275
   under the `production` section, replacing '<%= ENV["SECRET_KEY_BASE"] %>'.
276

277 278 279 280 281 282
3. Remove the `secret_token.rb` initializer.

4. Use `rake secret` to generate new keys for the `development` and `test` sections.

5. Restart your server.

283 284 285 286
### Changes to test helper

If your test helper contains a call to
`ActiveRecord::Migration.check_pending!` this can be removed. The check
287
is now done automatically when you `require 'rails/test_help'`, although
288 289
leaving this line in your helper is not harmful in any way.

290 291 292 293 294 295
### Cookies serializer

Applications created before Rails 4.1 uses `Marshal` to serialize cookie values into
the signed and encrypted cookie jars. If you want to use the new `JSON`-based format
in your application, you can add an initializer file with the following content:

296 297 298
```ruby
Rails.application.config.action_dispatch.cookies_serializer = :hybrid
```
299 300 301 302

This would transparently migrate your existing `Marshal`-serialized cookies into the
new `JSON`-based format.

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
When using the `:json` or `:hybrid` serializer, you should beware that not all
Ruby objects can be serialized as JSON. For example, `Date` and `Time` objects
will be serialized as strings, and `Hash`es will have their keys stringified.

```ruby
class CookiesController < ApplicationController
  def set_cookie
    cookies.encrypted[:expiration_date] = Date.tomorrow # => Thu, 20 Mar 2014
    redirect_to action: 'read_cookie'
  end

  def read_cookie
    cookies.encrypted[:expiration_date] # => "2014-03-20"
  end
end
```

It's advisable that you only store simple data (strings and numbers) in cookies.
If you have to store complex objects, you would need to handle the conversion
manually when reading the values on subsequent requests.

If you use the cookie session store, this would apply to the `session` and
`flash` hash as well.

327 328 329 330
### Flash structure changes

Flash message keys are
[normalized to strings](https://github.com/rails/rails/commit/a668beffd64106a1e1fedb71cc25eaaa11baf0c1). They
331
can still be accessed using either symbols or strings. Looping through the flash
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
will always yield string keys:

```ruby
flash["string"] = "a string"
flash[:symbol] = "a symbol"

# Rails < 4.1
flash.keys # => ["string", :symbol]

# Rails >= 4.1
flash.keys # => ["string", "symbol"]
```

Make sure you are comparing Flash message keys against strings.

G
Godfrey Chan 已提交
347 348
### Changes in JSON handling

349
There are a few major changes related to JSON handling in Rails 4.1.
G
Godfrey Chan 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

#### MultiJSON removal

MultiJSON has reached its [end-of-life](https://github.com/rails/rails/pull/10576)
and has been removed from Rails.

If your application currently depend on MultiJSON directly, you have a few options:

1. Add 'multi_json' to your Gemfile. Note that this might cease to work in the future

2. Migrate away from MultiJSON by using `obj.to_json`, and `JSON.parse(str)` instead.

WARNING: Do not simply replace `MultiJson.dump` and `MultiJson.load` with
`JSON.dump` and `JSON.load`. These JSON gem APIs are meant for serializing and
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).

#### JSON gem compatibility

Historically, Rails had some compatibility issues with the JSON gem. Using
`JSON.generate` and `JSON.dump` inside a Rails application could produce
unexpected errors.

Rails 4.1 fixed these issues by isolating its own encoder from the JSON gem. The
JSON gem APIs will function as normal, but they will not have access to any
Rails-specific features. For example:

```ruby
class FooBar
  def as_json(options = nil)
379
    { foo: 'bar' }
G
Godfrey Chan 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  end
end

>> FooBar.new.to_json # => "{\"foo\":\"bar\"}"
>> JSON.generate(FooBar.new, quirks_mode: true) # => "\"#<FooBar:0x007fa80a481610>\""
```

#### New JSON encoder

The JSON encoder in Rails 4.1 has been rewritten to take advantage of the JSON
gem. For most applications, this should be a transparent change. However, as
part of the rewrite, the following features have been removed from the encoder:

1. Circular data structure detection
2. Support for the `encode_json` hook
3. Option to encode `BigDecimal` objects as numbers instead of strings

397
If your application depends on one of these features, you can get them back by
G
Godfrey Chan 已提交
398 399 400
adding the [`activesupport-json_encoder`](https://github.com/rails/activesupport-json_encoder)
gem to your Gemfile.

401 402 403 404 405 406 407 408 409 410
#### JSON representation of Time objects

`#as_json` for objects with time component (`Time`, `DateTime`, `ActiveSupport::TimeWithZone`)
now returns millisecond precision by default. If you need to keep old behavior with no millisecond
precision, set the following in an initializer:

```
ActiveSupport::JSON::Encoding.time_precision = 0
```

411 412
### Usage of `return` within inline callback blocks

X
Xavier Noria 已提交
413
Previously, Rails allowed inline callback blocks to use `return` this way:
414 415 416

```ruby
class ReadOnlyModel < ActiveRecord::Base
X
Xavier Noria 已提交
417
  before_save { return false } # BAD
418 419 420 421 422
end
```

This behaviour was never intentionally supported. Due to a change in the internals
of `ActiveSupport::Callbacks`, this is no longer allowed in Rails 4.1. Using a
X
Xavier Noria 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436
`return` statement in an inline callback block causes a `LocalJumpError` to
be raised when the callback is executed.

Inline callback blocks using `return` can be refactored to evaluate to the
returned value:

```ruby
class ReadOnlyModel < ActiveRecord::Base
  before_save { false } # GOOD
end
```

Alternatively, if `return` is preferred it is recommended to explicitly define
a method:
437 438 439

```ruby
class ReadOnlyModel < ActiveRecord::Base
X
Xavier Noria 已提交
440
  before_save :before_save_callback # GOOD
441 442 443 444 445 446 447 448 449

  private
    def before_save_callback
      return false
    end
end
```

This change applies to most places in Rails where callbacks are used, including
X
Xavier Noria 已提交
450 451 452 453 454
Active Record and Active Model callbacks, as well as filters in Action
Controller (e.g. `before_action`).

See [this pull request](https://github.com/rails/rails/pull/13271) for more
details.
455

456 457 458 459 460 461 462 463 464 465
### Methods defined in Active Record fixtures

Rails 4.1 evaluates each fixture's ERB in a separate context, so helper methods
defined in a fixture will not be available in other fixtures.

Helper methods that are used in multiple fixtures should be defined on modules
included in the newly introduced `ActiveRecord::FixtureSet.context_class`, in
`test_helper.rb`.

```ruby
466
module FixtureFileHelpers
467 468 469 470 471 472
  def file_sha(path)
    Digest::SHA2.hexdigest(File.read(Rails.root.join('test/fixtures', path)))
  end
end
ActiveRecord::FixtureSet.context_class.send :include, FixtureFileHelpers
```
473

474 475
### I18n enforcing available locales

G
Guo Xiang Tan 已提交
476 477
Rails 4.1 now defaults the I18n option `enforce_available_locales` to `true`. This
means that it will make sure that all locales passed to it must be declared in
478 479 480 481 482 483 484 485 486
the `available_locales` list.

To disable it (and allow I18n to accept *any* locale option) add the following
configuration to your application:

```ruby
config.i18n.enforce_available_locales = false
```

G
Guo Xiang Tan 已提交
487 488
Note that this option was added as a security measure, to ensure user input
cannot be used as locale information unless it is previously known. Therefore,
G
Guo Xiang Tan 已提交
489 490
it's recommended not to disable this option unless you have a strong reason for
doing so.
491

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
### Mutator methods called on Relation

`Relation` no longer has mutator methods like `#map!` and `#delete_if`. Convert
to an `Array` by calling `#to_a` before using these methods.

It intends to prevent odd bugs and confusion in code that call mutator
methods directly on the `Relation`.

```ruby
# Instead of this
Author.where(name: 'Hank Moody').compact!

# Now you have to do this
authors = Author.where(name: 'Hank Moody').to_a
authors.compact!
```

509 510
### Changes on Default Scopes

A
Anton Cherepanov 已提交
511
Default scopes are no longer overridden by chained conditions.
512 513

In previous versions when you defined a `default_scope` in a model
A
Anton Cherepanov 已提交
514
it was overridden by chained conditions in the same field. Now it
515 516 517 518 519 520 521 522 523 524 525 526
is merged like any other scope.

Before:

```ruby
class User < ActiveRecord::Base
  default_scope { where state: 'pending' }
  scope :active, -> { where state: 'active' }
  scope :inactive, -> { where state: 'inactive' }
end

User.all
527
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
528 529

User.active
530
# SELECT "users".* FROM "users" WHERE "users"."state" = 'active'
531 532

User.where(state: 'inactive')
533
# SELECT "users".* FROM "users" WHERE "users"."state" = 'inactive'
534 535 536 537 538 539 540 541 542 543 544 545
```

After:

```ruby
class User < ActiveRecord::Base
  default_scope { where state: 'pending' }
  scope :active, -> { where state: 'active' }
  scope :inactive, -> { where state: 'inactive' }
end

User.all
546
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
547 548

User.active
549
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending' AND "users"."state" = 'active'
550 551

User.where(state: 'inactive')
552
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending' AND "users"."state" = 'inactive'
553 554 555 556 557 558 559 560 561
```

To get the previous behavior it is needed to explicitly remove the
`default_scope` condition using `unscoped`, `unscope`, `rewhere` or
`except`.

```ruby
class User < ActiveRecord::Base
  default_scope { where state: 'pending' }
A
Amit Thawait 已提交
562
  scope :active, -> { unscope(where: :state).where(state: 'active') }
563 564 565 566
  scope :inactive, -> { rewhere state: 'inactive' }
end

User.all
567
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
568 569

User.active
570
# SELECT "users".* FROM "users" WHERE "users"."state" = 'active'
571 572

User.inactive
573
# SELECT "users".* FROM "users" WHERE "users"."state" = 'inactive'
574 575
```

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
### Rendering content from string

Rails 4.1 introduces `:plain`, `:html`, and `:body` options to `render`. Those
options are now the preferred way to render string-based content, as it allows
you to specify which content type you want the response sent as.

* `render :plain` will set the content type to `text/plain`
* `render :html` will set the content type to `text/html`
* `render :body` will *not* set the content type header.

From the security standpoint, if you don't expect to have any markup in your
response body, you should be using `render :plain` as most browsers will escape
unsafe content in the response for you.

We will be deprecating the use of `render :text` in a future version. So please
start using the more precise `:plain:`, `:html`, and `:body` options instead.
Using `render :text` may pose a security risk, as the content is sent as
`text/html`.

595 596 597
### PostgreSQL json and hstore datatypes

Rails 4.1 will map `json` and `hstore` columns to a string-keyed Ruby `Hash`.
G
Guo Xiang Tan 已提交
598
In earlier versions, a `HashWithIndifferentAccess` was used. This means that
599 600 601 602
symbol access is no longer supported. This is also the case for
`store_accessors` based on top of `json` or `hstore` columns. Make sure to use
string keys consistently.

603 604
### Explicit block use for `ActiveSupport::Callbacks`

605 606
Rails 4.1 now expects an explicit block to be passed when calling
`ActiveSupport::Callbacks.set_callback`. This change stems from
607 608 609
`ActiveSupport::Callbacks` being largely rewritten for the 4.1 release.

```ruby
610
# Previously in Rails 4.0
611
set_callback :save, :around, ->(r, &block) { stuff; result = block.call; stuff }
612 613 614

# Now in Rails 4.1
set_callback :save, :around, ->(r, block) { stuff; result = block.call; stuff }
615 616
```

617 618 619 620 621 622 623
Upgrading from Rails 3.2 to Rails 4.0
-------------------------------------

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.

The following changes are meant for upgrading your application to Rails 4.0.

624 625
### HTTP PATCH

626 627 628 629
Rails 4 now uses `PATCH` as the primary HTTP verb for updates when a RESTful
resource is declared in `config/routes.rb`. The `update` action is still used,
and `PUT` requests will continue to be routed to the `update` action as well.
So, if you're using only the standard RESTful routes, no changes need to be made:
630 631 632 633 634

```ruby
resources :users
```

635 636 637
```erb
<%= form_for @user do |f| %>
```
638

639 640 641 642 643 644 645
```ruby
class UsersController < ApplicationController
  def update
    # No change needed; PATCH will be preferred, and PUT will still work.
  end
end
```
646

647 648
However, you will need to make a change if you are using `form_for` to update
a resource in conjunction with a custom route using the `PUT` HTTP method:
649

650 651 652 653 654
```ruby
resources :users, do
  put :update_name, on: :member
end
```
655 656 657 658 659

```erb
<%= form_for [ :update_name, @user ] do |f| %>
```

660 661 662
```ruby
class UsersController < ApplicationController
  def update_name
V
Vipul A M 已提交
663
    # Change needed; form_for will try to use a non-existent PATCH route.
664 665 666 667 668 669 670
  end
end
```

If the action is not being used in a public API and you are free to change the
HTTP method, you can update your route to use `patch` instead of `put`:

671
`PUT` requests to `/users/:id` in Rails 4 get routed to `update` as they are
672
today. So, if you have an API that gets real PUT requests it is going to work.
673 674
The router also routes `PATCH` requests to `/users/:id` to the `update` action.

675 676 677 678 679 680
```ruby
resources :users do
  patch :update_name, on: :member
end
```

681 682 683 684 685 686 687
If the action is being used in a public API and you can't change to HTTP method
being used, you can update your form to use the `PUT` method instead:

```erb
<%= form_for [ :update_name, @user ], method: :put do |f| %>
```

A
Aditya Kapoor 已提交
688
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/)
689 690 691 692
on the Rails blog.

#### A note about media types

693
The errata for the `PATCH` verb [specifies that a 'diff' media type should be
694 695 696 697 698 699 700 701 702 703
used with `PATCH`](http://www.rfc-editor.org/errata_search.php?rfc=5789). One
such format is [JSON Patch](http://tools.ietf.org/html/rfc6902). While Rails
does not support JSON Patch natively, it's easy enough to add support:

```
# in your controller
def update
  respond_to do |format|
    format.json do
      # perform a partial update
704
      @article.update params[:article]
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
    end

    format.json_patch do
      # perform sophisticated change
    end
  end
end

# In config/initializers/json_patch.rb:
Mime::Type.register 'application/json-patch+json', :json_patch
```

As JSON Patch was only recently made into an RFC, there aren't a lot of great
Ruby libraries yet. Aaron Patterson's
[hana](https://github.com/tenderlove/hana) is one such gem, but doesn't have
full support for the last few changes in the specification.

722 723
### Gemfile

724 725 726 727 728 729 730 731 732
Rails 4.0 removed the `assets` group from Gemfile. You'd need to remove that
line from your Gemfile when upgrading. You should also update your application
file (in `config/application.rb`):

```ruby
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
```
733

734
### vendor/plugins
735

736
Rails 4.0 no longer supports loading plugins from `vendor/plugins`. You must replace 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`.
737

738
### Active Record
739

740 741 742
* 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`.

* The `delete` method in collection associations can now receive `Fixnum` 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.
743

744 745
* 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.

746
* 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`.
747

748 749 750
* When using the default coder, assigning `nil` to a serialized attribute will save it
to the database as `NULL` instead of passing the `nil` value through YAML (`"--- \n...\n"`).

751
* 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.
752

753 754 755
* If you are not using Protected Attributes, you can remove any options related to
this gem such as `whitelist_attributes` or `mass_assignment_sanitizer` options.

756 757 758 759
* Rails 4.0 requires that scopes use a callable object such as a Proc or lambda:

```ruby
  scope :active, where(active: true)
760

761 762 763 764 765
  # becomes
  scope :active, -> { where active: true }
```

* Rails 4.0 has deprecated `ActiveRecord::Fixtures` in favor of `ActiveRecord::FixtureSet`.
V
Vipul A M 已提交
766

T
Trevor Turk 已提交
767 768
* Rails 4.0 has deprecated `ActiveRecord::TestCase` in favor of `ActiveSupport::TestCase`.

769 770 771 772 773 774 775 776 777 778 779 780
* Rails 4.0 has deprecated the old-style hash based finder API. This means that
  methods which previously accepted "finder options" no longer do.

* All dynamic methods except for `find_by_...` and `find_by_...!` are deprecated.
  Here's how you can handle the changes:

      * `find_all_by_...`           becomes `where(...)`.
      * `find_last_by_...`          becomes `where(...).last`.
      * `scoped_by_...`             becomes `where(...)`.
      * `find_or_initialize_by_...` becomes `find_or_initialize_by(...)`.
      * `find_or_create_by_...`     becomes `find_or_create_by(...)`.

781 782 783 784 785 786
* Note that `where(...)` returns a relation, not an array like the old finders. If you require an `Array`, use `where(...).to_a`.

* These equivalent methods may not execute the same SQL as the previous implementation.

* To re-enable the old finders, you can use the [activerecord-deprecated_finders gem](https://github.com/rails/activerecord-deprecated_finders).

787 788
### Active Resource

J
Jeff Dickey 已提交
789
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.
790

791
### Active Model
792

793
* 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`.
794

795
* 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 behaviour. This means that you can comment or remove the following option in the `config/initializers/wrap_parameters.rb` file:
796 797 798 799 800 801 802

```ruby
# Disable root element in JSON by default.
# ActiveSupport.on_load(:active_record) do
#   self.include_root_in_json = false
# end
```
803

804
### Action Pack
805

806 807 808 809 810 811 812 813 814 815
* Rails 4.0 introduces `ActiveSupport::KeyGenerator` and uses this as a base from which to generate and verify signed cookies (among other things). 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`.

```ruby
  # config/initializers/secret_token.rb
  Myapp::Application.config.secret_token = 'existing secret token'
  Myapp::Application.config.secret_key_base = 'new secret key base'
```

Please note that 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. This is because cookies signed based on the new `secret_key_base` in Rails 4.x are not backwards compatible with Rails 3.x. You are free to leave your existing `secret_token` in place, not set the new `secret_key_base`, and ignore the deprecation warnings until you are reasonably sure that your upgrade is otherwise complete.

816
If you are relying on the ability for external applications or Javascript to be able to read your Rails app's signed session cookies (or signed cookies in general) you should not set `secret_key_base` until you have decoupled these concerns.
817

818
* 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.
819

820 821
Please read [Pull Request #9978](https://github.com/rails/rails/pull/9978) for details on the move to encrypted session cookies.

822
* Rails 4.0 removed the `ActionController::Base.asset_path` option. Use the assets pipeline feature.
823

824
* Rails 4.0 has deprecated `ActionController::Base.page_cache_extension` option. Use `ActionController::Base.default_static_extension` instead.
825

826
* Rails 4.0 has removed Action and Page caching from Action Pack. You will need to add the `actionpack-action_caching` gem in order to use `caches_action` and the `actionpack-page_caching` to use `caches_pages` in your controllers.
827

828
* Rails 4.0 has removed the XML parameters parser. You will need to add the `actionpack-xml_parser` gem if you require this feature.
829

830 831
* Rails 4.0 changes the default memcached client from `memcache-client` to `dalli`. To upgrade, simply add `gem 'dalli'` to your `Gemfile`.

832
* 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.
833

834 835 836 837 838
* Rails 4.0 deprecates the `:confirm` option for the `link_to` helper. You should
instead rely on a data attribute (e.g. `data: { confirm: 'Are you sure?' }`).
This deprecation also concerns the helpers based on this one (such as `link_to_if`
or `link_to_unless`).

839
* Rails 4.0 changed how `assert_generates`, `assert_recognizes`, and `assert_routing` work. Now all these assertions raise `Assertion` instead of `ActionController::RoutingError`.
840

841
* 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`:
842 843 844 845 846 847 848 849 850 851 852

```ruby
  get 'one' => 'test#example', as: :example
  get 'two' => 'test#example', as: :example
```

```ruby
  resources :examples
  get 'clashing/:id' => 'test#example', as: :example
```

853 854 855 856
In the first case, you can simply avoid using the same name for multiple
routes. In the second, you can use the `only` or `except` options provided by
the `resources` method to restrict the routes created as detailed in the
[Routing Guide](routing.html#restricting-the-routes-created).
857

858
* Rails 4.0 also changed the way unicode character routes are drawn. Now you can draw unicode character routes directly. If you already draw such routes, you must change them, for example:
859

860
```ruby
861
get Rack::Utils.escape('こんにちは'), controller: 'welcome', action: 'index'
862
```
863 864 865

becomes

866
```ruby
867
get 'こんにちは', controller: 'welcome', action: 'index'
868
```
869

870
* Rails 4.0 requires that routes using `match` must specify the request method. For example:
871 872 873

```ruby
  # Rails 3.x
874
  match '/' => 'root#index'
875 876

  # becomes
877
  match '/' => 'root#index', via: :get
878 879

  # or
880
  get '/' => 'root#index'
881 882
```

883
* 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`.
884 885 886 887 888 889 890 891 892 893

Remember you must also remove any references to the middleware from your application code, for example:

```ruby
# Raise exception
config.middleware.insert_before(Rack::Lock, ActionDispatch::BestStandardsSupport)
```

Also check your environment settings for `config.action_dispatch.best_standards_support` and remove it if present.

B
Brian Alexander 已提交
894
* In Rails 4.0, precompiling assets no longer automatically copies non-JS/CSS assets from `vendor/assets` and `lib/assets`. Rails application and engine developers should put these assets in `app/assets` or configure `config.assets.precompile`.
895

896
* In Rails 4.0, `ActionController::UnknownFormat` is raised when the action doesn't handle the request format. By default, the exception is handled by responding with 406 Not Acceptable, but you can override that now. In Rails 3, 406 Not Acceptable was always returned. No overrides.
897

898
* In Rails 4.0, a generic `ActionDispatch::ParamsParser::ParseError` exception is raised when `ParamsParser` fails to parse request params. You will want to rescue this exception instead of the low-level `MultiJson::DecodeError`, for example.
899

900 901
* In Rails 4.0, `SCRIPT_NAME` is properly nested when engines are mounted on an app that's served from a URL prefix. You no longer have to set `default_url_options[:script_name]` to work around overwritten URL prefixes.

T
Trevor Turk 已提交
902 903 904 905 906 907 908 909 910
* Rails 4.0 deprecated `ActionController::Integration` in favor of `ActionDispatch::Integration`.
* Rails 4.0 deprecated `ActionController::IntegrationTest` in favor of `ActionDispatch::IntegrationTest`.
* Rails 4.0 deprecated `ActionController::PerformanceTest` in favor of `ActionDispatch::PerformanceTest`.
* Rails 4.0 deprecated `ActionController::AbstractRequest` in favor of `ActionDispatch::Request`.
* Rails 4.0 deprecated `ActionController::Request` in favor of `ActionDispatch::Request`.
* Rails 4.0 deprecated `ActionController::AbstractResponse` in favor of `ActionDispatch::Response`.
* Rails 4.0 deprecated `ActionController::Response` in favor of `ActionDispatch::Response`.
* Rails 4.0 deprecated `ActionController::Routing` in favor of `ActionDispatch::Routing`.

911
### Active Support
912

913
Rails 4.0 removes the `j` alias for `ERB::Util#json_escape` since `j` is already used for `ActionView::Helpers::JavaScriptHelper#escape_javascript`.
914

915
### Helpers Loading Order
916

917
The order in which helpers from more than one directory are loaded has changed in Rails 4.0. Previously, they were gathered and then sorted alphabetically. After upgrading to Rails 4.0, helpers will preserve the order of loaded directories and will be sorted alphabetically only within each directory. Unless you explicitly use the `helpers_path` parameter, this change will only impact the way of loading helpers from engines. If you rely on the ordering, you should check if correct methods are available after upgrade. If you would like to change the order in which engines are loaded, you can use `config.railties_order=` method.
918

919 920 921 922
### Active Record Observer and Action Controller Sweeper

Active Record Observer and Action Controller Sweeper have been extracted to the `rails-observers` gem. You will need to add the `rails-observers` gem if you require these features.

923 924
### sprockets-rails

925 926
* `assets:precompile:primary` and `assets:precompile:all` have been removed. Use `assets:precompile` instead.
* The `config.assets.compress` option should be changed to `config.assets.js_compressor` like so for instance:
927 928 929 930

```ruby
config.assets.js_compressor = :uglifier
```
931

932 933
### sass-rails

934
* `asset-url` with two arguments is deprecated. For example: `asset-url("rails.png", image)` becomes `asset-url("rails.png")`.
935

936 937
Upgrading from Rails 3.1 to Rails 3.2
-------------------------------------
938

G
George Ogata 已提交
939 940
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.
941

G
George Ogata 已提交
942 943
The following changes are meant for upgrading your application to the latest
3.2.x version of Rails.
944

945
### Gemfile
946

947
Make the following changes to your `Gemfile`.
948

949
```ruby
G
George Ogata 已提交
950
gem 'rails', '3.2.18'
951 952

group :assets do
953 954
  gem 'sass-rails',   '~> 3.2.6'
  gem 'coffee-rails', '~> 3.2.2'
955 956
  gem 'uglifier',     '>= 1.0.3'
end
957
```
958

959
### config/environments/development.rb
960

961
There are a couple of new configuration settings that you should add to your development environment:
962

963
```ruby
964 965 966 967 968 969
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict

# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5
970
```
971

972
### config/environments/test.rb
973

974
The `mass_assignment_sanitizer` configuration setting should also be be added to `config/environments/test.rb`:
975

976
```ruby
977 978
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
979
```
980

981
### vendor/plugins
982

983
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`.
984

985 986 987 988
### Active Record

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.

989 990
Upgrading from Rails 3.0 to Rails 3.1
-------------------------------------
991

992
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.
993

994
The following changes are meant for upgrading your application to Rails 3.1.12, the last 3.1.x version of Rails.
995

996
### Gemfile
997

998
Make the following changes to your `Gemfile`.
999

1000
```ruby
1001
gem 'rails', '3.1.12'
1002 1003 1004 1005
gem 'mysql2'

# Needed for the new asset pipeline
group :assets do
1006 1007 1008
  gem 'sass-rails',   '~> 3.1.7'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier',     '>= 1.0.3'
1009 1010 1011 1012
end

# jQuery is the default JavaScript library in Rails 3.1
gem 'jquery-rails'
1013
```
1014

1015
### config/application.rb
1016

1017
The asset pipeline requires the following additions:
1018

1019
```ruby
1020 1021
config.assets.enabled = true
config.assets.version = '1.0'
1022
```
1023

1024
If your application is using an "/assets" route for a resource you may want change the prefix used for assets to avoid conflicts:
1025

1026
```ruby
1027 1028
# Defaults to '/assets'
config.assets.prefix = '/asset-files'
1029
```
1030

1031
### config/environments/development.rb
1032

1033
Remove the RJS setting `config.action_view.debug_rjs = true`.
1034

1035
Add these settings if you enable the asset pipeline:
1036

1037
```ruby
1038 1039 1040 1041 1042
# Do not compress assets
config.assets.compress = false

# Expands the lines which load the assets
config.assets.debug = true
1043
```
1044

1045
### config/environments/production.rb
1046

1047
Again, most of the changes below are for the asset pipeline. You can read more about these in the [Asset Pipeline](asset_pipeline.html) guide.
1048

1049
```ruby
1050
# Compress JavaScripts and CSS
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
config.assets.compress = true

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

# Generate digests for assets URLs
config.assets.digest = true

# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH

# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
1067
```
1068

1069
### config/environments/test.rb
1070

1071 1072
You can help test performance with these additions to your test environment:

1073
```ruby
1074 1075
# Configure static asset server for tests with Cache-Control for performance
config.serve_static_assets = true
1076
config.static_cache_control = 'public, max-age=3600'
1077
```
1078

1079
### config/initializers/wrap_parameters.rb
1080

1081
Add this file with the following contents, if you wish to wrap parameters into a nested hash. This is on by default in new applications.
1082

1083
```ruby
1084 1085 1086 1087 1088 1089
# Be sure to restart your server when you modify this file.
# This file contains settings for ActionController::ParamsWrapper which
# is enabled by default.

# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
1090
  wrap_parameters format: [:json]
1091 1092 1093 1094 1095 1096
end

# Disable root element in JSON by default.
ActiveSupport.on_load(:active_record) do
  self.include_root_in_json = false
end
1097
```
1098

1099
### config/initializers/session_store.rb
1100 1101 1102

You need to change your session key to something new, or remove all sessions:

1103
```ruby
1104
# in config/initializers/session_store.rb
1105
AppName::Application.config.session_store :cookie_store, key: 'SOMETHINGNEW'
1106
```
1107 1108 1109

or

1110
```bash
1111
$ bin/rake db:sessions:clear
1112
```
1113 1114 1115 1116

### Remove :cache and :concat options in asset helpers references in views

* With the Asset Pipeline the :cache and :concat options aren't used anymore, delete these options from your views.