LoginSignup
0
1

More than 3 years have passed since last update.

rails:ページ移行で、(navbarなど)リンク表示を変更する

Posted at

いらないリンクを消したい時

ログインページにいるのにログインリンク。新規登録ページにいるのにsignup(新規登録)のリンクはいらないですよね。
僕も今回、学習段階で実装する場面が来たので簡単にご紹介します。

・新規登録ページ

navbarのリンクはlogin表示のみ。signup(新規登録)リンクは隠す
スクリーンショット 2020-02-22 19.25.25.png

・ログインページ

navbarのリンクはsignup(新規登録)表示のみ。loginリンクは隠す
スクリーンショット 2020-02-22 19.04.45.png

該当コード

request:ユーザのヘッダー情報や環境変数を取得

#新規登録画面におけるnavbarのコード

<% unless request.path.include?("login") %> #"login"とのurlを含まなければtrue
    <li class="nav-item"><%= link_to 'Log in', login_path, class:'nav-link' %></li>
<% end %>


#ログイン画面におけるnavbarのコード

<% unless request.path.include?("users/new") %> #"users/new"とのurlを含まなければtrue
    <li class="nav-item"><%= link_to 'Sign up', new_user_path, class:'nav-link'%></li>
<% end %>
#"signup"または"users/new"とのurlを含まなければtrue

<% unless request.path.include?("signup") || request.path.include?("users/new")%>
    <li class="nav-item"><%= link_to 'Sign up', new_user_path, class:'nav-link'%></li>
<% end %>

補足

足りない部分や、間違っている箇所、もっときれいにコードを書ける部分があればご指摘いただきたいです。

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