在 Rails 3 中突出显示当前页面的最佳方法?-- 有条件地将 css 类应用于链接 [英] Best way to highlight current page in Rails 3? -- apply a css class to links conditionally
本文介绍了在 Rails 3 中突出显示当前页面的最佳方法?-- 有条件地将 css 类应用于链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于以下代码:
<%= link_to "Some Page", some_path %>
如何使用 current_page?
辅助方法应用 css 类 current
?
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
In app/helpers/application_helper.rb
def cp(path)
"current" if current_page?(path)
end
在您看来:
<%= 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" %>
这篇关于在 Rails 3 中突出显示当前页面的最佳方法?-- 有条件地将 css 类应用于链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文