LoginSignup
0
0

Rails 7.1 では存在しないアクションをコールバックに指定できない

Posted at

Rails 7.1 では,before_action などのコールバック指定で,存在しないアクションを only パラメーターの値に含めることができない。

たとえば,

config/routes.rb
resources :items

というようなアプリがあったとする。
要するに Item に対する基本的な操作ができるようなもの。

そして,ItemsController のほうで,

class ItemsController < ApplicationController
  before_action :set_item, only: %i[ show edit update destroy ]

  # 中略

  private 

  def set_item
    @item = Item.find(params[:id])
  end
end

のようにコールバックを設定するとする。

ここで,仕様の変更によって,Item の削除機能を省くことになったとする。
すると,まずルーティングのほうは

config/routes.rb
resouces :items, except: %i[ destroy ]

のように,destory アクションは無い,ということにしておいて,さらに ItemsController の destroy メソッドを削除することになる。

問題はコントローラーの

  before_action :set_item, only: %i[ show edit update destroy ]

の部分だ。
もはや destroy アクションは無いので,only の指定から destroy を取り除かなければならない。

しかし,Rails 7.0 までは取り除き忘れてもとくに問題は起きなかった。

これが,Rails 7.1 になると,エラーが起きるようになった。

Rails バージョンを上げたらテストが通らなくなったが,当初何が起こっているのか全く分からず時間を溶かしてしまった。
Rails サーバーで動作を確認してもやはり何かがおかしい。
基本に立ち返ってログをきちんと見たらちゃんと書いてあった。
つまり

AbstractController::ActionNotFound (The destroy action could not be found for the :set_item callback on ItemsController, but it is listed in the controller's :only option.

Raising for missing callback actions is a new default in Rails 7.1, if you'd like to turn this off you can delete the option from the environment configurations or set `config.action_controller.raise_on_missing_callback_actions` to `false`.
):

であると。
要するに,コントローラーの :only オプションにリストアップされている destroy アクションは見つからん,これで例外を出してほしくないならコレコレせよ,と。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0