Rails 4 / Bootstrap 3: how to display a different navigation bar on different pages?

穿精又带淫゛_ 提交于 2019-11-30 23:08:56

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 %>

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'.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!