如何以 DRY 方式基于 current_page 将“活动"类应用于我的导航?- 导轨 3 [英] How do I apply an 'active' class to my navigation based on the current_page in a DRY way? - Rails 3

查看:33
本文介绍了如何以 DRY 方式基于 current_page 将“活动"类应用于我的导航?- 导轨 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在我的 application.html.erb 中,我的导航结构如下所示:

但是,我想做的是,一旦它们位于导航中的任何页面上,它就会将 active 类应用于相应的链接.

例如,如果用户在 mydomain.com/johnbrown 上,也就是 Profile 链接,rails 助手看起来像这样:

link_to "Profile", vanity_path(:vname => current_user.username), :class =>活动".

但是我如何以编程方式做到这一点,这样我才不会复制内容?即我如何为导航中的所有页面获取该功能并尽可能地编写它?

谢谢.

解决方案

这是一个非常好的问题.我从来没有对我看到或提出的解决方案感到满意.也许我们可以在这里聚一聚.

这是我过去尝试过的

我制作了一个帮助程序,它返回一个哈希值,因为我使用了 HAML 定义了 :class

def active_tab(path)request.path.match(/^#{path}/) ?{ :class =>'积极的' } : {}结尾

前用法:

= link_to "Dashboard", dashboard_path, active_tab("#{dashboard_path}$")

或者类似的替代方案

def active_class(path)request.path =~/#{path}/?'活跃' : 无结尾

前用法:

= link_to 'Presentations', admin_presentations_path, :class =>"#{active_class('presentations')}"

我希望看到其他一些关于此的建议.

So in my application.html.erb I have my navigational structure that looks something like this:

<div id="navigation">
            <ul class="pills">
                    <% if current_page?(:controller => 'welcome', :action => 'index') %>
                        <li><%= link_to "Profile", vanity_path(:vname => current_user.username) %></li>
                        <li><%= link_to "Settings", settings_path %></li>
                        <li><%= link_to "Sign Out", signout_path %></li>
                    <% elsif !current_page?(:controller => 'welcome', :action => 'index') %>
                        <li><%= link_to "Home", root_path %></li>
                        <li><%= link_to "Profile", vanity_path(:vname => current_user.username) %></li>
                        <li><%= link_to "Settings", settings_path %></li>
                        <li><%= link_to "Sign Out", signout_path %></li>              
                    <% end %>
            </ul>
        </div>

However, what I would like to do, is once they are on any of the pages in the navigation, it applies a class active to the respective link.

So for instance, if the user is on mydomain.com/johnbrown which is the Profile link, the rails helper would look something like this:

link_to "Profile", vanity_path(:vname => current_user.username), :class => "active".

But how do I do that in a programmatic way, so I am not duplicating content? i.e. how do I get that functionality for all the pages in my navigation and write it as DRY as possible?

Thanks.

解决方案

This is a really great question. I've never really been happy with the solutions I've seen or come up with. Maybe we can get one together here.

Here is what I've tried in the past

I've made a helper that returns a hash with :class defined since I use HAML

def active_tab(path)
  request.path.match(/^#{path}/) ? { :class => 'active' } : {}
end

ex usage:

= link_to "Dashboard", dashboard_path, active_tab("#{dashboard_path}$")

Or an alternative along the same lines

def active_class(path)
  request.path =~ /#{path}/ ? 'active' : nil
end

ex usage:

= link_to 'Presentations', admin_presentations_path, :class => "#{active_class('presentations')}"

I would love to see some other suggestions on this.

这篇关于如何以 DRY 方式基于 current_page 将“活动"类应用于我的导航?- 导轨 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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