16
10

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.

current_page?で場合分け ❏Rails❏

Last updated at Posted at 2019-12-03

newとeditで同一の部分テンプレートを利用しているが、それぞれちょっとだけ変えたい時

current_page?(URL)で指定したページにいるか判定してくれます。

_form.html.haml
- if current_page?(new_tweet_path)
  %p 新規作成
- elsif current_page?("/tweets/#{tweet.id}/edit")
  %p 編集

= form_with model: tweet, local: true do |form|
...
new.html.haml
= render "form", tweet: @tweet
edit.html.haml
= render "form", tweet: @tweet

newページでは新規作成が、
editページでは編集が表示されるようになりました。


`(URL)`の部分は以下が可能みたいです。 エラーが出たらいくつか試してみてください。

###・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")


参考 >http://shonoooo.hatenablog.com/entry/2017/10/31/232302
ではまた!
16
10
2

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
16
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?