LoginSignup
1
0

More than 3 years have passed since last update.

[Rails] view パスで条件分岐する方法 [current_page?]

Last updated at Posted at 2021-03-04

current_page?

current_page?は、表示中のページのパスを判定できるメソッドです。
これはUrlHelperとして実装されているため、ビューの中から呼び出すことができます。

・URL

current_page?(http://hoge.com/hoge)

・パス

current_page?(/tweets/new)

・prefix

current_page?(new_tweet_path)

・アクション指定

current_page?(action: "new")

・コントローラー&アクション指定

current_page?(controller: "tweets", action: "new")


show.html.erb
<% if current_page?(tweet_path(@tweet)) %>

<% end %>

のような感じで、パスで条件分岐することが可能です。

筆者は同クラスでshowページを2画面作りたくてこの書き方をしました。
あまり良くない書き方の場合は、ご指摘いただけますと幸いです。


こちら
の記事を参照させていただきました。

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