LoginSignup
4
3

More than 5 years have passed since last update.

Rails で flash メッセージを JavaScript で表示する [notify]

Posted at

問題

Rails だと、

redirect_to root_path, notice: 'なんやて?'

みたいな感じでページ上部になんやて?を表示するのが標準(多分)のやり方だが、そうじゃなくて JS で notify とか pnotify で表示したいよーって時に困った。

ググってみるとヘッダーから取り出す記事があったりしたが、よく考えたらめちゃくちゃ簡単に解決できた。

解決法

こっそり画面上部にメッセージを隠す。

<body>
  <div id="flash-notice" hidden>
    <%= flash[:notice] %>
  </div>
    etc...
</body>

そして JS から使う。

$.notify($('#flash-notice').text().trim());

結論

幸せになった。

4
3
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
4
3