Anyway to write this code better?

Hi Guys,

I’m using this helper

def nav_link_to(link, url, opts={})
    if current_resource.url == url_for(url)
      prefix = '<li class="active">'
    else
      prefix = '<li>'
    end
    prefix + link_to(link, url, opts) + "</li>"
  end

It works great with this one

= nav_link_to '<i class="icon block layout green"></i>Dashboard', 'admin-live-dashboard.html'
= nav_link_to '<i class="icon block layout green"></i>Design Template', 'admin-live-design-template.html'

But is there any way I can write the code better and cleaner. This part is really way off to look at

nav_link_to '<i class="icon block layout green"></i>Dashboard',

Thanks in advance!

Of course :smile:

You can try this

= nav_link_to 'admin-live-dashboard.html' do
  <i class="icon block layout green"></i>
  Dashboard'

For your project i think middleman-navtree can help you.
If you are looking for code example you can hack here https://github.com/flexbox/foundation-boilerplate

Hi Flexbox,

Thanks for the reply, not sure if it’s working though.
It gives out an error.

ArgumentError at /admin-live-dashboard.html
wrong number of arguments (1 for 2)

Ruby /Users/davidmandeeli/SHIRO/Work/DigitalKickStart/webinarignition/config.rb: in nav_link_to, line 90
Web GET localhost/admin-live-dashboard.html

Here’s the original helper code

def nav_link_to url, name
     path = request.path
     class_name = (path == url) ? ' class="active"' : ''
     "<li#{class_name}><a href=\"#{url}\">#{name}</a></li>"
   end

I think I’ve got what I need.

I modified the old def code. Here’s the new one

def nav_link_to name, icon, url
    path = request.path
    class_name = (path == url) ? ' class="active"' : ''
    "<li#{class_name}><a href=\"#{url}\"><i class=\"icon #{icon}\"></i>#{name}</a></li>"
  end

codes in the nav.haml

  = nav_link_to 'Dashboard', 'block layout', 'admin-live-dashboard.html'

which outputs

<li class="active"><a href="admin-live-dashboard.html"><i class="icon block layout"></i>Dashboard</a></li>

Anyway, thanks again!