LoginSignup
0
0

More than 3 years have passed since last update.

RailsでFlashメッセージを表示させる

Posted at

はじめに

前回の続きで「Ruby on Rails 5 速習実践ガイド」のアウトプットを行うための投稿です。
今回は、ページ遷移をするときにFlashメッセージを表示させる方法についてお伝えします!

目次

  1. redirect
  2. render
  3. メッセージを表示
  4. reference

redirect_to

redirect_toを以下のように記述します。

redirect_to パス, notice: "#{name}を登録しました"

notice以外は、デフォルトでalertを渡すこともできます。

redirect_to パス, alert: "登録失敗です"

render

renderの場合は、以下の記述です。

flash.now[:alert] = "nameを入力してください。"
render :new

メッセージを表示

遷移先のテンプレートで以下のように記述し、表示します。

- if flash.notice.present?
  = flash.notice

すべてのFlashをページ上部で表示させる想定であれば、application.html.slimにこちらを記述するとスッキリします!

参考文献

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