添加“当前”的最佳方式Rails 3 中的类到导航
我的导航菜单中有一些静态页面。我想向当前显示的项目添加一个像“current”这样的类。
我这样做的方法是添加大量的辅助方法(每个方法对应一个项目)来检查控制器和操作。
def current_root_class
'class="current"' if controller_name == "homepage" && action_name == "index"
end
<ul>
<li <%= current_root_class %>><%= link_to "Home", root_path %>
还有更好的办法吗!?我现在的做法实在是太愚蠢了……
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......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
我制作了一个名为
nav_link
的助手:使用方式如下:
它将生成类似的 HTML
I made a helper called
nav_link
:used like:
which will produce HTML like
使用
current_page?
帮助器以确定是否应该分配"current"
类。例如:请注意,您还可以传递路径(不仅仅是选项的哈希值),例如:
current_page?(root_path)
。Use the
current_page?
helper to determine whether or not you should assign the"current"
class. For example:Note you can also pass a path (not only a hash of options), e.g:
current_page?(root_path)
.这里并不是真正的答案,因为我使用的方式与你完全相同。我刚刚定义了辅助方法来测试多个控制器或操作:
在 application_helper.rb 中
然后您可以使用
if controller?("homepage") && action?("index", "show")
在你的视图或其他辅助方法中......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:
In application_helper.rb
Then you can use
if controller?("homepage") && action?("index", "show")
in your views or other helper methods…我在 application_helper.rb (Rails 3) 中使用这个 nav_link(text, link) 函数来完成工作,它会为我滚动我的 bootstrap twitter 2.0 导航栏链接。
例子:
I use this nav_link(text, link) function in application_helper.rb (Rails 3) to get the job done and it rolls my bootstrap twitter 2.0 nav bar links for me.
Example:
我这样做的方法是在 application_helper 中添加一个辅助函数
,然后在导航中,
这会根据当前页面 uri 测试链接路径,并返回当前类或空字符串。
我还没有对此进行彻底的测试,而且我对 RoR 还很陌生(在使用 PHP 十年后才转向),所以如果这有一个重大缺陷,我很乐意听到它。
至少这样你只需要 1 个辅助函数和每个链接中的一个简单调用。
The way I've done it is to add a helper function in the application_helper
Then in the nav,
This tests the link path against the current page uri and returns either your current class or an empty string.
I've not tested this thoroughly and I'm very new to RoR (moving over after a decade with PHP) so if this has a major flaw I'd love to hear it.
At least this way you only need 1 helper function and a simple call in each link.
为了构建@Skilldrick的答案...
如果您将此代码添加到application.js,它将确保任何具有活动子项的下拉菜单也将被标记为活动...
回顾一下支持代码>添加一个名为 nav_link:
used like:
的帮助器,它将生成类似的 HTML
To build off @Skilldrick 's answer...
If you add this code to application.js it will make sure that any dropdown menus with active children will also be marked as active...
To recap supportive code > Add a helper called nav_link:
used like:
which will produce HTML like
我认为最好的方法是
application_helper.rb:
和在菜单中:
I think the best way is
application_helper.rb:
And in menu:
我知道这是一个过时的答案,但是您可以使用名为 的 link_to 帮助器包装器轻松忽略所有这些当前页面检查active_link_to gem,它完全按照你想要的方式工作,将活动类添加到当前页面链接
I know it is a out dated answer, but you can easily ignore all these current page check by using a link_to helper wrapper, called active_link_to gem, it works exactly what you want, add a active class to current page link
这是完整的示例,介绍如何在 Rails 视图的引导菜单页面上添加活动类。
Here is the full example, on how to add an active class on bootstrap menu page in rails view.
我使用一个名为 Tabs on Rails 的很棒的 gem。
I use an awesome gem called Tabs on Rails.
我有一个更简洁的 nav_link 版本,其工作方式与 link_to 完全相同,但经过定制以输出包装 li 标记。
将以下内容放入 application_helper.rb
如果您查看上面的代码并将其与 url_helper.rb 中的 link_to 代码进行比较,唯一的区别是它检查 url 是否是当前页面,并将类“active”添加到一个包装 li 标签。这是因为我将 nav_link 帮助器与 Twitter Bootstrap 的 nav 组件 一起使用,它更喜欢链接被包装在 li 标签内,“active”类应用于外部 li。
上面代码的好处是它允许您将块传递到函数中,就像使用 link_to 一样。
例如,带有图标的引导导航列表如下所示:
Slim:
输出:
此外,就像 link_to 帮助器一样,您可以将 HTML 选项传递到 nav_link 中,该选项将应用于 a 标记。
为锚点传递标题的示例:
Slim:
输出:
I have a more succinct version of nav_link that works exactly like link_to, but is customized to output a wrapping li tag.
Put the following in your application_helper.rb
If you look at the above code and compare it to the link_to code in url_helper.rb, the only difference is that it checks if the url is the current page, and adds the class "active" to a wrapping li tag. This is because I'm using the nav_link helper with Twitter Bootstrap's nav component which prefers links to be wrapped inside li tags and the "active" class applied to the outer li.
The nice thing about the above code is that it allows you to pass in a block into the function, just like you can do with link_to.
For example, a bootstrap nav list with icons would look like:
Slim:
Output:
In addition, just like the link_to helper, you can pass in HTML options into nav_link, which will be applied to the a tag.
An example of passing in a title for the anchor:
Slim:
Output:
就我个人而言,我在这里使用了答案的组合
,我使用的是 Materialize CSS,我使主要类别可折叠的方法是使用下面的代码
希望它可以帮助某人
For me personally i used a combination of answers here
I am using materialize css and my way of making the main categories collapsible is by using the code below
hope it helps someone
current_page?
方法对我来说不够灵活(假设你设置了一个控制器而不是一个操作,那么它只会在控制器的索引操作上返回 true),所以我做了这个基于关于其他答案:示例:
The
current_page?
method isn't flexible enough for me (say you set a controller but not an action, then it'll only return true on the controller's index action), so I've made this based on the other answers:Example:
是的!查看这篇文章:将“选定”类添加到 Rails 中的链接的更好方法
将 nav_link_helper.rb 放入 app/helpers 中,它可以像以下一样简单:
nav_link 帮助程序的工作方式与标准 Rails link_to 帮助程序类似,但如果满足某些条件,则将“选定”类添加到您的链接(或其包装器)中都满足了。默认情况下,如果链接的目标 url 与当前页面的 url 相同,则会向链接添加默认类“selected”。
这里有一个要点: https://gist.github.com/3279194
更新:这现在是一个宝石:http://rubygems.org/gems/nav_link_to
Yep! Check out this article: A Better Way to Add a ‘selected’ Class to Links in Rails
Drop nav_link_helper.rb into app/helpers and it can be as easy as:
The nav_link helper works just like the standard Rails link_to helper, but adds a 'selected' class to your link (or its wrapper) if certain criteria are met. By default, if the link's destination url is the same url as the url of the current page, a default class of 'selected' is added to the link.
There's a gist here: https://gist.github.com/3279194
UPDATE: This is now a gem: http://rubygems.org/gems/nav_link_to
我使用像这样的简单助手作为顶级链接,因此
/stories/my-story
页面突出显示/stories
链接I use a simple helper like this for top level links so the
/stories/my-story
page highlights the/stories
link让我展示我的解决方案:
_header.html.erb:
application_helper.rb:
application_controller.rb:
Let me show my solution:
_header.html.erb:
application_helper.rb:
application_controller.rb:
根据Skilldrick的回答,我将其更改为以下内容:
使其更有用。
According to the answer by Skilldrick, I'll change it to the following:
to make it much more useful.
此版本基于@Skilldrick 的版本,但允许您添加 html 内容。
因此,您可以执行:
nav_link "A Page", a_page_path
还可以:
或任何其他 html 内容(例如,您可以添加图标)。
这里的助手是:
This version is based on @Skilldrick's one but allows you to add html content.
Thus, you can do:
nav_link "A Page", a_page_path
but also:
or any other html content (you can add an icon for instance).
Here the helper is:
我想我想出了一个简单的解决方案,可能对很多用例都有帮助。这让我:
link_to
内的 HTML(例如,在链接内添加图标)application_helper.rb
active
到链接元素的整个类名,而不是它是唯一的类。因此,将其添加到
application_helper.rb
中:在你的模板上你可以有这样的东西:
作为奖励,您可以指定或不指定
class_name
并像这样使用它:感谢之前的回答1,2 和 资源。
I think I came up with a simple solution that might be helpful for a lot of use cases. This lets me:
link_to
(e.g. add an icon inside the link)application_helper.rb
active
to the whole class name of the link element instead of it being the sole class.So, add this to
application_helper.rb
:And on your template you can have something like this:
As bonus you can specify or not a
class_name
and use it like this:<div class="<%= current_page?(root_path) %>">
Thanks to previous answers 1, 2 and resources.
在
ApplicationHelper
中创建一个方法,如下所示。现在在视图中使用它,如下用例。
1. 对于任何特定控制器的所有操作
2. 对于许多控制器的所有操作(以逗号分隔)
3. 对于任何特定控制器的特定操作
4. 对于许多控制器的特定操作(以逗号分隔)
5 .对于任何特定控制器的一些特定操作
6.对于许多控制器的一些特定操作(以逗号分隔)
希望有帮助
Create a method in
ApplicationHelper
as below.Now use it in view as below use cases.
1. For all action of any specific controller
2. For all action of many controllers (with comma seperated)
3. For specific action of any specific controller
4. For specific action of many controllers (with comma seperated)
5. For some specific actions of any specific controller
6. For some specific actions of many controllers (with comma seperated)
Hope it helps
所有这些都适用于简单的导航栏,但是下拉子菜单怎么样?
当选择子菜单时,顶部菜单项应设为“当前”
在这种情况下 tabs_on_rails 我就是解决方案
all these work with simple nav bars, but what about drop down sub-menu ?
when a sub-menu is selected the top menu item should be made 'current'
in this case tabs_on_rails me be the solution
这就是我在当前项目中解决的方法。
然后..
This is how I solved in my current project.
Then..
我的简单方法 -
application.html.erb
,main_controller.erb
,谢谢。
My easy way -
application.html.erb
,main_controller.erb
,Thanks.
如果您还想在视图中支持 html 选项哈希。例如,如果你想用其他 CSS 类或 id 来调用它,你可以像这样定义辅助函数。
因此,在视图中,以与调用 link_to helper 相同的方式调用此 helper
If also you want to support html options hash in the view. For example if you want to call it with other CSS class or id, you can define the helper function like this.
So in the view, call this helper the same way you'd call link_to helper