Rails 4 / Bootstrap 3: how to display a different

2019-02-15 10:33发布

问题:

Here is the _header.html.erb of our Rails 4 app, made with Bootstrap 3 components:

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="container">
    <nav>
      <div class="col-xs-4">
        <%= link_to "APP NAME", root_path, id: "logo" %>
      </div>
      <div class="col-xs-4 text-center">
        <ul class="nav navbar-nav navbar-center">
          <li><%= link_to "Features",   features_path %></li>
          <li><%= link_to "Pricing",   pricing_path %></li>
          <li><%= link_to "Blog", '#' %></li>
        </ul>
      </div>
      <div class="col-xs-4">
        <ul class="nav navbar-nav navbar-right">
          <% if current_user.try(:admin?) %>
            <li><%= link_to "Users", users_path %></li>
          <% end %>
          <% if logged_in? %>
            <li><%= link_to "Dashboard", current_user %></li>
            <li><%= link_to "Settings", current_user %></li>
            <li><%= link_to "Log out", logout_path, method: "delete" %></li>
        <% else %>
          <li><%= link_to "Log in", login_path %></li>
          <li><%= link_to "Sign up", signup_path %></li>
        <% end %>
        </ul>
      </div>
    </nav>
  </div>
</header>

As you can see, this conditionally displays a navigation bar, depending on whether the current user is logged out, logged in or logged in as admin.

However, we are wondering how we could display a different navigation bar on different pages.

For instance, on all public pages (home, features, pricing, blog, help, etc.), we would like to display the Features, Pricing & Blog links (whether the user is logged in or not) but on the inner pages of the app (dashboard, settings, etc) we would like to remove these links.

EDIT: to make things clearer, what we call the public pages are actually our static_pages, which rely on our StaticPages#Controller, while the inner app pages such as dashboard and settings rely on our Users#Controller.

How can we achieve this? Do we need to create a new _header.html.erb partial?

Is there a particular Rails way to do that?

回答1:

You could put each variation of the navbar in it's own partial and make use of content_for.

In your application layout you could have logic that checks if a specific navbar should be rendered, like this:

<% if content_for?(:navbar) %>
    <%= yield(:navbar) %>
<% else %>
   # code for default navbar
<% end %>

And inside your views, where you want the different navbars

 <% content_for :navbar do %>
      <%= render 'nav_bar_variation_one' %>
 <% end %>

and

 <% content_for :navbar do %>
      <%= render 'nav_bar_variation_two' %>
 <% end %>


回答2:

In my case i wanted a different font color for my navbar. So it was too complex to create an other navbar.

Here my solution:

i created a new methode which will return 'black-navbar' if i'm into my orders_controller or 'white-navbar' in the others cases.

#my code into application_helper.rb
def navbar_attr
    if %w(orders).include?(params[:controller])
      return 'black-navbar'
    else
      return 'white-navbar'
    end
end

then i will call this methode into the html of my navbar

# in my case my file is _navbar.html.erb 
<p class="<%= navbar_attr %>">Hello</p> 

If my navbar is call via my orders_controller it will add the class 'black-navbar' in the others cases it will add 'white-navbar'.