#author("2018-02-27T22:05:38+09:00","default:wikiwriter","wikiwriter")
&tag(Rails/アップグレード/4.2から5.0);
*目次 [#l9a304dd]
#contents
*関連ページ [#he8e43a0]
*参考情報 [#ic168dd4]

*Rails 4.2からRails 5.0 [#kae9c16c]

**手順 [#s9cb922d]
-GemfileのRailsバージョンをRails.5.0系の最新版に書き換えてbundle updateを実行。
 bundle update
-設定ファイルの更新。古い設定ファイルを上書きしようとしてくるので基本Yesで応える。後からgit diffで差分を見ながら移植したほうがよさげ。
 bundle exec rails app:update
-IntelliJの場合Compare with Branchで比較する(Commitだと変更を戻したりできない)。routes.rbなど適宜commitしたバージョンを戻していく。
-app/models/application_record.rbを作成
#pre{{
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end
}}
-各モデルクラスが継承するようにする(必須ではない)。
#pre{{
class Book < ApplicationRecord
end

}}

**移行後のWarningつぶし [#ye6eeaa1]
-デバッグ実行 or テストを実行してWarningをつぶしていく
***MySQLの絵文字 [#ib17db7f]
-MySQLで絵文字を使っている場合はar_innodb_row_format.rbの書き換えが必要。
***before_filter is deprecated [#o702369a]
-before_actionに書き換える。

***Rails Controller Testingのエラー [#oe8e550f]
-controllerのテストでassignを使っている場合以下のようなエラーが表示される
#pre{{
NoMethodError: assigns has been extracted to a gem. To continue using it,
        add `gem 'rails-controller-testing'` to your Gemfile.
}}
-Gemfileにrails-controller-testingを追加する
#pre{{
group :test do
  gem "rails-controller-testing"
end
}}
-.DEPRECATION WARNING: Using positional arguments in functional tests has been deprecated,
-以下のようなエラー。
#pre{{
.DEPRECATION WARNING: Using positional arguments in functional tests has been deprecated,
in favor of keyword arguments, and will be removed in Rails 5.1.

Deprecated style:
get :show, { id: 1 }, nil, { notice: "This is a flash message" }

New keyword style:
get :show, params: { id: 1 }, flash: { notice: "This is a flash message" },
  session: nil # Can safely be omitted.
}}
-指示通りにparamsやflashなどのキーワードを追加する。
#pre{{
      post :create, params: {book: { author: @book.author, summary: @book.summary, title: @book.title } }
    get :show, params: {id: @book}
}}

***Mime::JSON to: Mime::Type[:JSON] [#t31298d2]
-[[Deprecated warning - Mime::JSON · Issue #789 · rbenv/rbenv · GitHub:https://github.com/rbenv/rbenv/issues/789]]によるとjbuilderのwarningらしい。
-Rails 5.0.x系の場合jbuilderの指定は以下がデフォルト
#pre{{
gem 'jbuilder', '~> 2.5'
}}

***take_paramsの書き換え [#r3d2681f]
-take_paramsで「DEPRECATION WARNING: Method update is deprecated and will be removed in Rails 5.1」というエラーが表示される。
-ApplicationController::ParametersがHashを継承しなくなるための警告らしい。to_unsafe_hをはさんだけどこれでいいのか不明。
#pre{{
  def take_params(*param_keys)
    overwrites = param_keys.extract_options!
    param_keys = default_take_param_keys if param_keys.blank?
    params.to_unsafe_h.extract!(*param_keys).update(overwrites)
  end
  helper_method :take_params
}}

***Protected Attributesの廃止 [#h4997113]
-Rails 4では[[GitHub - rails/protected_attributes: Protect attributes from mass-assignment in ActiveRecord models.:https://github.com/rails/protected_attributes]]が使えたがRails 5では使えないので、Strong Parameterに対応しないといけない。
-CarrierWaveをStrong Parameter対応にするには、:icon、icon_cache、:remove_iconなども含めないと行けない。

***XMLシリアライズ [#z495c8e8]
-RailsのActiveModel::Serializers::Xmlが外部に移転。to_xmlを使い続けたい場合Gemfileに以下の行を追加する。
 gem 'activemodel-serializers-xml'

***ActiveRecord::Base.raise_in_transactional_callbacks= is deprecated [#g572f0a1]
-environment.rbの以下の行で発生
 require File.expand_path('../application', __FILE__)
-実際はapplication.rbに含まれる以下の行を削除する。
  config.active_record.raise_in_transactional_callbacks = true

***Using a dynamic :action segment in a route is deprecated and will be removed in Rails 5.2. [#id4ea7c3]
-routes.rbから、以下のような部分を削除orコメントアウト
  # get ':namespace:controller(/:action(/:id))(.:format)'

***Minitestの実行後レポート表示時にエラー [#z79cc56a]
-2017/05/10(水)現在、Rails 5.0.2でrailtiesとminitestのaggregated_resultsの引数が合致していない状態になっている。
-[[- Write aggregated_results directly to the IO object to avoid mixed e… · seattlerb/minitest@c6ba2af · GitHub:https://github.com/seattlerb/minitest/commit/c6ba2afd90473b76d289562edd24f7d7ca8484f9]]。
-とりあえずtest_helper.rbに以下を追加すればごまかせる。
#pre{{
module Minitest
  class SuppressedSummaryReporter < SummaryReporter
    def aggregated_results(*)
      super unless options[:output_inline]
    end
  end
end

}}

***kind_of?(Hash)の変換 [#edaa3bfd]
-ActionController::ParametersがHashを継承しなくなった。Rails 5では以下のような判定にする。
 kind_of?(Hash) #Rails 4まで
 kind_of?(ActionController::Parameters) #Rails 5まで



トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS