使用 rails中的current_page? 方法 ,以及自制方法:
<a href="/" class='<%= (request.original_fullpath.in? ['/', ''] ) ? 'active' : ''%>' >首页</a>
<% Sort.where('parent_id is null').each do |sort| %>
<% url = "/pages/details?sort_id=#{sort.id}" %>
# 自制方法:
<%# link_to sort.name, url, :class => url == request.original_fullpath ? 'active' : '' %>
# rails的 current_page? 方法
<%= link_to sort.name, url, :class => current_page?(url) ? 'active' : '' %>
<% end %>
还是 current_page? 省事儿。 因为自制RAILS方法的话,需要查询各种不同版本的request 获取 request_uri, full_url 等方法。见: http://stackoverflow.com/questions/2165665/how-do-i-get-the-current-absolute-url-in-ruby-on-rails
另外,还可以分别使用 js , css/html 的方式来实现,见: http://www.apaddedcell.com/highlight-links-current-page-css