在 Rails 3 的菜单部分中渲染活动菜单元素
我有一个根据用户所在页面呈现的动态子菜单。我将以下代码放置在 _sub_menu.html.erb 部分中:
<a href="/dashboard/index" class="current">Index</a>
<a href="/dashboard/account">Account</a>
<a href="/dashboard/payments">Payments</a>`
从我的主视图中,我调用 <%= render 'sub_menu' %>
,它有效。
但是,我想根据用户所在的页面更改 class="current" 部分。我希望通过传入本地参数并根据该参数进行渲染来从渲染中执行此操作,但这似乎很老套:
<%= render 'sub_menu' , :locals =>; {:active_item =>; '付款'} %>
另外,逻辑变得非常丑陋。有更好的方法吗?
I have a dynamic submenu that is rendered according to which page the user is on. I placed the following code in a _sub_menu.html.erb partial:
<a href="/dashboard/index" class="current">Index</a>
<a href="/dashboard/account">Account</a>
<a href="/dashboard/payments">Payments</a>`
From my my main view, I call <%= render 'sub_menu' %>
, which works.
However, I want to change the class="current" part according to which page the user is on. I was hoping to do this from the render by passing in a local parameter and rendering according to that, but it seems hacky:
<%= render 'sub_menu' , :locals => {:active_item => 'payments'} %>
Plus the logic becomes really ugly. Is there a better way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以定义一个额外的帮助器方法,该方法将调用 link_to_unless_current :
然后从导航部分调用它:
You can define an additional helper method, that would be calling link_to_unless_current:
and then call it from you navigation partial:
我认为您可以使用块参数
link_to_unless_current
,它提供当链接是当前页面时要呈现的内容:
这有效吗?
I think you can make use of the block parameter to
link_to_unless_current
, which provides the content to render when the link is the current page:Does that work?