-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Navbar Helper
It should let you write things like:
<%= nav_bar :fixed => :top, :brand => "Fashionable Clicheizr 2.0", :responsive => true do %>
<%= menu_group do %>
<%= menu_item "Home", root_path %>
<%= menu_divider %>
<%= drop_down "Products" do %>
<%= menu_item "Things you can't afford", expensive_products_path %>
<%= menu_item "Things that won't suit you anyway", harem_pants_path %>
<%= menu_item "Things you're not even cool enough to buy anyway", hipster_products_path %>
<% if current_user.lives_in_hackney? %>
<%= menu_item "Bikes", fixed_wheel_bikes_path %>
<% end %>
<% end %>
<%= menu_item "About Us", about_us_path %>
<%= menu_item "Contact", contact_path %>
<% end %>
<%= menu_group :pull => :right do %>
<% if current_user %>
<%= menu_item "Log Out", log_out_path %>
<% else %>
<%= form_for @user, :url => session_path(:user), html => {:class=> "navbar-form pull-right"} do |f| -%>
<p><%= f.text_field :email %></p>
<p><%= f.password_field :password %></p>
<p><%= f.submit "Sign in" %></p>
<% end -%>
<% end %>
<% end %>
<% end %>
In your view file (most likely application.html.erb) to get a basic navbar set up you need to do this:
<%= nav_bar %>
Which will render:
<div class="navbar">
<div class="container">
</div>
</div>
If you want the navbar to stick to the top of the screen, pass in the option like this:
<%= nav_bar :fixed => :top %>
To render:
<div class="navbar navbar-fixed-top">
<div class="container">
</div>
</div>
If you want a full-width navbar that scrolls away with the page, pass in the option like this:
<%= nav_bar :static => :top %>
To render:
<div class="navbar navbar-static-top">
<div class="container">
</div>
</div>
Add the name of your site on the left hand edge of the navbar. By default, it will link to root_url. Passing a brand_link option will set the url to whatever you want.
<%= nav_bar :brand => "We're sooo web 2.0alizr", :brand_link => account_dashboard_path %>
Which will render:
<div class="navbar">
<div class="container">
<a class="navbar-brand" href="/accounts/dashboard">
We're sooo web 2.0alizr
</a>
</div>
</div>
If you want the responsive version of the navbar to work (One that shrinks down on mobile devices etc.), you need to pass this option:
<%= nav_bar :responsive => true %>
Which renders the html quite differently:
<div class="navbar">
<div class="container">
<!-- .navbar-toggle is used as the toggle for collapsed navbar content -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Everything in here gets hidden at 940px or less -->
<div class="navbar-collapse collapse">
<!-- menu items gets rendered here instead -->
</div>
</div>
</div>
This is the 'meat' of the code where you define your menu items.
You can group menu items in theoretical boxes which you can apply logic to - e.g. show different collections for logged in users/logged out users, or simply right align a group.
The active menu item will be inferred from the link for now.
The important methods here are menu_group and menu_item.
menu_group only takes one argument - :pull - this moves the group left or right when passed :left or :right.
menu_item generates a link wrapped in an li tag. It takes two arguments and an options hash. The first argument is the name (the text that will appear in the menu), and the path (which defaults to "#" if left blank). The rest of the options are passed straight through to the link_to helper, so that you can add classes, ids, methods or data tags etc.
<%= nav_bar :fixed => :top, :brand => "Ninety Ten" do %>
<%= menu_group do %>
<%= menu_item "Home", root_path %>
<%= menu_item "About Us", about_us_path %>
<%= menu_item "Contact", contact_path %>
<% end %>
<% if current_user %>
<%= menu_item "Log Out", log_out_path %>
<% else %>
<%= menu_group :pull => :right do %>
<%= menu_item "Sign Up", registration_path %>
<%= form_for @user, :url => session_path(:user) do |f| -%>
<p><%= f.text_field :email %></p>
<p><%= f.password_field :password %></p>
<p><%= f.submit "Sign in" %></p>
<% end -%>
<% end %>
<% end %>
<% end %>
For multi-level list options, where it makes logical sense to group menu items, or simply to save space if you have a lot of pages, you can group menu items into drop down lists like this:
<%= nav_bar do %>
<%= menu_item "Home", root_path %>
<%= drop_down "Products" do %>
<%= menu_item "Latest", latest_products_path %>
<%= menu_item "Top Sellers", popular_products_path %>
<%= drop_down_divider %>
<%= menu_item "Discount Items", discounted_products_path %>
<% end %>
<%= menu_item "About Us", about_us_path %>
<%= menu_item "Contact", contact_path %>
<% end %>
Dividers are just vertical bars that visually separate logically disparate groups of menu items
<%= nav_bar :fixed => :bottom do %>
<%= menu_item "Home", root_path %>
<%= menu_item "About Us", about_us_path %>
<%= menu_item "Contact", contact_path %>
<%= menu_divider %>
<%= menu_item "Edit Profile", edit_user_path(current_user) %>
<%= menu_item "Account Settings", edit_user_account_path(current_user, @account) %>
<%= menu_item "Log Out", log_out_path %>
<% end %>
At the moment - this is just a how to...
You need to add this class to the form itself (Different form builders do this in different ways - please check out the relevant docs)
.navbar-form
To pull the form left or right, add either of these classes:
.pull-left
.pull-right
If you want the Bootstrap search box (I think it just rounds the corners), use:
.navbar-search
Instead of:
.navbar-form
To change the size of the form fields, use .span2 (or however many span widths you want) to the input itself.
You can shift things to the left or the right across the nav bar. It's easiest to do this on grouped menu items:
<%= nav_bar :fixed => :bottom do %>
<% menu_group do %>
<%= menu_item "Home", root_path %>
<%= menu_item "About Us", about_us_path %>
<%= menu_item "Contact", contact_path %>
<% end %>
<% menu_group :pull => :right do %>
<%= menu_item "Edit Profile", edit_user_path(current_user) %>
<%= menu_item "Account Settings", edit_user_account_path(current_user, @account) %>
<%= menu_item "Log Out", log_out_path %>
<% end %>
<% end %>
If you want to put regular plain text in the navbar anywhere, you do it like this:
<%= nav_bar :brand => "Apple" do %>
<%= menu_text "We make shiny things" %>
<%= menu_item "Home", root_path %>
<%= menu_item "About Us", about_us_path %>
<% end %>
It also takes the :pull option to drag it to the left or right.