0
0

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でJavaScriptでの遷移時にフラッシュを表示させる

Last updated at Posted at 2020-02-16

やりたいこと

RailsでJavaScriptで遷移させるときに、フラッシュを表示させたい。
調べるとフラッシュに相当する文言を隠して( display: none とか?) おいて、JavaScriptで表示させる( display: block とか)の方法しかなかった。
GETパラメータで送るのヤダなぁと思ったので別の方法を考えた。

やったこと

アクションを作ってフラッシュ付きでリダイレクトさせる

具体的にはこう。

items.js
location.href = '/items/redirect_with_flash_message';
items_controller.rb
class ItemsController < WebController
  def redirect_with_flash_message
    flash[:alert] = 'ログインが必要です'

    redirect_to new_user_session_path
  end
end

どうでもいいけどRailsのタグ、スペル間違ってない?

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?