2
1

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】[戻る]ボタンを押すとJSのコードが画面に表示されて画面遷移が正常にされない場合の対処法

Last updated at Posted at 2020-10-28

環境

rails 6.0.3

説明

ajax化した画面を表示後に、別画面へ遷移し、その後ブラウザの戻るボタンで画面を戻ると、
生のjsコードが画面に表示される問題について。

ブラウザのキャッシュが残っていることが原因だったので、解決方法をメモとして下記に記載します。

該当のcontroller
#---------*** 前略 ***---------

  before_action :set_cache_headers

  #-------*** 中略 ***--------

  private

  def set_cache_headers
    response.headers["Cache-Control"] = "no-store"
  end

#---------*** 後略 ***---------

該当のcontrollerに上記を追加することで解決しました。
自分の場合、no-storeのみの指定で解決しましたが、これが正しいのかどうかが確かで無いので誤っていたらコメントをお願いします。

参考記事

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?