2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails 「redirect_to :back」 エラー解消

Posted at

【Rails 「redirect_to :back」 エラー解消】

Ruby on Rails 6で学習している際にこのエラーに遭遇。
自分なりの備忘録です。

今回、エラーになった構文

ログイン機能を実装した際に、リダイレクトが出来なかった。

**「redirect_to :back」**で直前のページにリダイレクトしたかったが、エラーになった。
 → Ruby on Rails 4以前のバージョンでの構文らしい。
 → Ruby on Rails 6では使えないようだ。

Rails
  redirect_to :back, flash: {
    user: user,
    error_messages: user.errors.full_messages

エラーの対処法

Ruby on Rails 5以降では、下記のように変更する。
「redirect_back (fallback_location:)」が追加された。

自分は、下記のように変更した。
「redirect_to :back」→「redirect_back fallback_location: root_path」

Railsガイド 参照。「redirect_to :back」は非推奨である。

Rails
  redirect_back fallback_location: root_path, flash: {
    user: user,
    error_messages: user.errors.full_messages

おわりに

バージョンの違いでエラーに遭遇することがよくある。
エラーが出たら、Google検索などで冷静に対処していく。
自走力を身につけていこう。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?