写出来的都是shit,快关闭!

Rails为当前页面添加class="active"标记

application_helper.rb

1
2
3
4
5
6
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

_narbar.html.slim

1
= nav_link ' 首 页 ', root_path

效果

1
<li class="active"><a href="/"> 首 页 </a></li>