添加“当前"的最佳方式在 Rails 3 中导航的类 [英] Best way to add "current" class to nav in Rails 3

查看:18
本文介绍了添加“当前"的最佳方式在 Rails 3 中导航的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在导航菜单中有一些静态页面.我想为当前显示的项目添加一个类似current"的类.

I have some static pages in a navigation menu. I want to add a class like "current" to the item which is currently displaying.

我这样做的方式是添加大量辅助方法(每个用于一项)来检查控制器和操作.

The way I am doing so is to add tons of helper methods (each for one item) to check the controller and action.

def current_root_class
  'class="current"' if controller_name == "homepage" && action_name == "index" 
end

<ul>
  <li <%= current_root_class %>><%= link_to "Home", root_path %>

有没有更好的方法呢!?我现在的方式太傻了……

Is there any better way to do so!? My current way is so stupid......

推荐答案

这里不是真正的答案,因为我使用的方式和你完全一样.我刚刚定义了辅助方法来测试多个控制器或操作:

Not truly an answer here, because I'm using quite the same way as you are. I've just defined helper methods to test for multiple controller or actions:

在 application_helper.rb 中

In application_helper.rb

  def controller?(*controller)
    controller.include?(params[:controller])
  end

  def action?(*action)
    action.include?(params[:action])
  end

那么你可以使用 if controller?("homepage") &&action?("index", "show") 在你的视图或其他辅助方法中......

Then you can use if controller?("homepage") && action?("index", "show") in your views or other helper methods…

这篇关于添加“当前"的最佳方式在 Rails 3 中导航的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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