Rails: Simple nav link helper


/ Published in: Rails
Save to your folder(s)

In an effort to stay DRY, here's a simple way to organize your main navigation.


Copy this code and paste it in your HTML
  1. #1. Insert this in /rails_app/helpers/application_helper.rb
  2. def nav_link( text, controller, action="index" )
  3. link_to_unless_current text, :controller => controller,
  4. :action => action
  5. end
  6.  
  7. #2. Make a Call from /views/layouts/application_helper.rb
  8. <div id="nav">
  9. <%= nav_link "Home", "site" %>
  10. <%= nav_link "About", "site", "about" %>
  11. <%= nav_link "Help", "site", "help" %>
  12. </div>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.