LoginSignup
14
12

More than 3 years have passed since last update.

【Rails】redirect_toで変数やflashを渡す

Last updated at Posted at 2020-01-10

redirect_toでページ遷移する先に、変数やflashを引き渡す方法がわかったのでメモ。

変数

送る側

redirect_to hoge_url(user_id: @customer.id)

受け取る側

user_id = params[:user_id]

flash

alert, notice

redirect_to hoge_url, alert: 'エラーが発生しました'
redirect_to hoge_url, notice: '登録しました'

それ以外

redirect_to hoge_url, flash: { key: 'message' }
redirect_to hoge_url, flash: { info: 'メールを送信しました' }

おまけ

以下のような書き方もできる

redirect_to '/user/show'

参考

flashを使って簡易メッセージを表示する

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