navigationruby-on-rails-3.2urlhelpercurrent-page

How To add more element to nav_link on current_page?


I have some static pages in a navigation menu. I want to add more element to the item which is currently displaying.

navigation on view layout/adminzor.html.erb

<ul class='main-nav'>
    <%= nav_link 'Dashboard', adminzor_path %>
</ul>

on application_helper.rb

def nav_link(link_text, link_path)
  class_name = current_page?(link_path) ? 'active' : ''
    content_tag(:li, :class => class_name) do
      link_to link_text, link_path
    end
 end

and output

<ul class='main-nav'>
   <li class="active">
      <a href="/adminzor">Dashboard</a>
   </li>
</ul>

because I'm using the nav_link helper with Twitter Bootstrap's nav component which prefers links to be wrapped inside li tags and the "active" class applied to the outer li. and i add some element on view such as

<%= nav_link adminzor_path, :class => "light" do %>
  <div class="ico"><i class="icon-home icon-white"></i></div>
  Dashboard
<% end %>

and helper

def nav_link(link_text, link_path)
      class_name = current_page?(link_path) ? 'active' : ''
        content_tag(:li, :class => class_name) do
          link_to link_path do
           link_text
          end
        end
   end

but error

ArgumentError in Adminzor/dashboards#index

Showing c:/Sites/zionrails/app/views/layouts/adminzor.html.erb where line #44 raised:

wrong number of arguments (3 for 2)

i want to output such as

<ul class='main-nav'>
<li class="active">
 <a href="/adminzor" class="light">
    <div class="ico">
       <i class="icon-home icon-white"></i>
    </div>Dashboard
 </a>
</li>
</ul>

Are there any other solutions? thank's


Solution

  • solved

    application_helper.rb

    def is_active?(link_path)
      if current_page?(link_path)
        "active"
      else
        ""
      end
    end
    

    and on layout/adminzor.html.erb such as

    <li class="<%= is_active?(adminzor_path) %>">
                    <%= link_to adminzor_path, :class => 'light' do %>
                    <div class="ico"><i class="icon-home icon-white"></i></div>
                    Dashboard
                    <% end %>
    </li>