Navigation

adrianmacneil edited this page Sep 9, 2011 · 8 revisions

The Navigation widgets works pretty much like the Tabnav but generates navigation links instead of tabs.

How to generate Navigation links

Let’s generate a main tabbed navigation for our site (I’ll call it main, you can call it what you want)

$ script/generate navigation main
      create  app/views/widgets
      create  app/views/widgets/_main_navigation.html.erb

It creates a partial with the navigation definition.
Now we can insert the navigation wherever we want, I’ll put it in my app/views/layouts/application.html.erb.

...
<%= navigation :main %>	
<%= yield %>
...

Reload your pages, you should see top right navigation links.
Now you can edit the app/views/widgets/_main_navigation.html.erb partial:

...
  add_item do |i|
    i.named "home"
    i.links_to :controller => "invoices"
  end
  ...
  add_item :link => "http://www.seesaw.it"

Example: Welcome message, help, and logout

...
add_item do |i|
  i.named "#{current_user.name}'s account"
  i.links_to :controller => 'account'
end if current_user

add_item do |i|
  i.named "welcome guest"
  i.disable! # the link is not clickable
end

add_item do |i|
  i.named "help"
  i.links_to :controller => 'help'
  i.new_window true
end

add_item do |i|
  i.named "logout"
  i.function_to 'App.logOutAction();'
end if logged_in?

Why should I use this?

The Navigation widgets offers a couple of benefits over standard links:

  • you can disable items with i.disable!
  • you can highlight the items using the same Tabnav highlighting system.

If you do not use these features, It’s probably better to use plain links.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.