1

我在导航菜单中有一些静态页面。我想为当前显示的项目添加更多元素。

视图布局/adminzor.html.erb 上的导航

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

在 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

和输出

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

因为我将 nav_link 助手与 Twitter Bootstrap 的 nav 组件一起使用,该组件更喜欢将链接包装在 li 标签内,并将“活动”类应用于外部 li。我在视图上添加了一些元素,例如

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

和帮手

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

但错误

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)

我想输出比如

<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>

还有其他解决方案吗?谢谢

4

2 回答 2

1
  def menu_link(link_text, icon_type, link_path)
    class_name = current_page?(link_path) ? 'active' : ''

    content_tag(:li, :class => class_name) do
      link_to link_path do
        content_tag(:i, '', :class => icon_type) + link_text
      end
    end

  end

和电话:

<%= menu_link('My lists', 'icon-list', user_lists_path(@user)) %>
于 2013-05-07T14:43:55.217 回答
0

解决了

application_helper.rb

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

在 layout/adminzor.html.erb 上,例如

<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>
于 2013-01-11T09:07:10.180 回答