在Rails 3中突出显示当前页面的最佳方法?(Best way to highlight current page in Rails 3? — apply a css class to links conditionally)
对于以下代码:
<%= link_to "Some Page", some_path %>如何使用current_page? helper方法应用current的css类?
或者如果还有其他更好的方法可用?
For the following code:
<%= link_to "Some Page", some_path %>How do I apply a css class current using the current_page? helper method?
Or if some other better way is available?
最满意答案
在app / helpers / application_helper.rb中
def cp(path) "current" if current_page?(path) end在你的意见:
<%= link_to "All Posts", posts_path, class: cp(posts_path) %>基本上写一个简单的包装。 此外,您可以扩展该方法,以允许通过添加参数应用其他类。 保持意见简洁/干燥。 或者,没有扩展方法,你可以像简单的String插入一样添加额外的类:
<%= link_to "All Posts", posts_path, class: "#{cp(posts_path)} additional_class" %>In app/helpers/application_helper.rb
def cp(path) "current" if current_page?(path) endIn your views:
<%= link_to "All Posts", posts_path, class: cp(posts_path) %>Basically write a simple wrapper around it. Additionally you could extend the method to allow additional classes to be applied by adding arguments. Keeps the views concise/dry. Or, without extending the method, you could just do simple String interpolation like so to add additional classes:
<%= link_to "All Posts", posts_path, class: "#{cp(posts_path)} additional_class" %>更多推荐
发布评论