#author("2017-05-09T22:58:41+09:00","default:wikiwriter","wikiwriter")
&tag(Rails/アップグレード);
*目次 [#i7b29fc5]
#contents
*関連ページ [#ka78e350]
*参考情報 [#b73bc7b2]
-[[Rails アップグレードガイド | Rails ガイド:https://railsguides.jp/upgrading_ruby_on_rails.html]]
-[[Rails 5へのアップグレード手順メモ(Rails 4.2.6 => 5.0.0) - Qiita:http://qiita.com/ryo511/items/2a387df126268fec8c78]]
-[[ダメ男のブログ: 【Rails5.0.0】 rails4.2.5からrails5.0.0にアップグレードする その1 Gemと設定ファイルの修正:http://edywrite.blogspot.jp/2016/07/rails500-rails425rails500-1-gem.html]]
-[[ダメ男のブログ: 【Rails5.0.0】 Rails4.2.5からRails5.0.0へのアップグレードでバッチが動かなくなった:http://edywrite.blogspot.jp/2016/07/rails500-rails425rails500.html]]

*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'

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS