向rails中的所有活动链接添加“活动”类? [英] Add an 'active' class to all active links in rails?

查看:137
本文介绍了向rails中的所有活动链接添加“活动”类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有很多代码看起来像这样:

Basically, I have a lot of code that looks like this:

link_to t('.profile'), business_path(@business), class: '#{'active' if current_page? business_path(@business)}'

这不是很干。

我想知道如果有人知道一个好的方法来修改link_to帮助器本身,以自动添加一个活动类到当前页面的所有链接。

I was wondering if anyone knows a good way to modify the link_to helper itself to automatically add an 'active' class to all links to the current page.

如果它有帮助,我可以使用HAML或SLIM。

If it helps, I'm open to using HAML or SLIM.

推荐答案

你自己的包装link_to的助手。在你的application_helper.rb中,你可以写一个方法 active_link_to ,它使用与link_to + current_page相同的参数,然后只是调用link_to,就像你在做上面一样。

This is a good case for writing your own helper that wraps the link_to. In your application_helper.rb you can write a method active_link_to that takes the same params as link_to + current_page, and then just calls link_to like you are doing above.

这篇关于向rails中的所有活动链接添加“活动”类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆