1
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 1 year has passed since last update.

🍪初回だけ表示させるページを実装

Last updated at Posted at 2023-07-04

:cherries:学んだことを随時まとめてく:cherries:
~間違っている点があれば是非教えてください~

やりたいこと:cloud::cloud_rain::sunny:

:cloud::sunny:ページに初めて遷移する前に別の:cloud_rain:ページを表示させたい

使うもの

  • session
    セッション機能を利用してユーザーの一時的なデータの保存を行う
  • if
    セッションの真偽値で遷移ページを分岐させる
  • and return
    if文の中にrenderを複数使用するならアクションを明示的に終了させる

実装

controller.rb
def cloud
    session[:キー名] = true          #1.sessionの初期化/初回だよ〜ってデータ持たせる
end

def rain
    session[:キー名] = false         #3.遷移したら初回じゃないよ〜ってデータ持たせる
end

def sunny
    if session[キー名] == true       #2.初回ならrainページ遷移
      render rain_path and return 
    end    
end
cloud.html.erb
<%= link_to sunny_path %>           #1回目も2回目もsunnyアクションへ行くことがpoint

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