Display the navigation bar on all pages except the main page.

I want to show two different navigators. One will be displayed on all pages except the home page when you are logged in. For now, another navigation bar will only appear on my landing page.

I think I will probably need to write an if statement.

If (current user is not logged in) or maybe (current user is viewing home page) do
<nav>Second navbar</nav>
else
<nav>First navbar</nav>
end

I am very new to rails, so I could be wrong. (And yes, I know that it’s not the right way to write an if statement in Ruby)

The homepage is located at:

Home / index.html.erb

+4
source share
3 answers

I usually do the following settings:

  • create partial shared/_nav_menu.html.erb

inside partial input logical logic:

<% if current_user %>
  // nav bar for logged in user
<% else %>
 // nav bar for non logged in users
<% end %>

application.html.erb partial :

<%= render :partial => 'shared/_nav_menu' if show_menu? %>

application_controller :

def show_menu?
  true
end

helper method: show_menu?

static_pages, static_pages_controller show_menu? false.

class StaticPagesController < ApplicationController
  def show_menu? 
    false
  end

  helper_method: show_menu?
end

, , nav menu . nav menu .

application.html.erb if..else.

+2

, , , , , "".

:

<%= render :partial => @nav_bar_partial %>

:

def standard_nav
  @nav_bar_partial = "path/to/standard/nav/partial"
end

, ( ) .

@nav_bar_partial = "path/to/new/nav/partial" if condition_that_requires_a_different_nav

, . - - , , - .

+1

Source: https://habr.com/ru/post/1651445/


All Articles