在 Rails 3 的菜单部分中渲染活动菜单元素

发布于 2024-09-28 14:58:55 字数 541 浏览 0 评论 0原文

我有一个根据用户所在页面呈现的动态子菜单。我将以下代码放置在 _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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫腻 2024-10-05 14:58:55

您可以定义一个额外的帮助器方法,该方法将调用 link_to_unless_current

def link_to_current_with_class(name, current_class, options = {}, html_options = {}, &block)
  link_to_unless_current(name, options, html_options) do
    options[:class] = current_class + " " + options[:class]
    link_to(name, options, html_options)
  end
end

然后从导航部分调用它:

<%= link_to_current_with_class "Index", "current", "/dashboard/index" %>

You can define an additional helper method, that would be calling link_to_unless_current:

def link_to_current_with_class(name, current_class, options = {}, html_options = {}, &block)
  link_to_unless_current(name, options, html_options) do
    options[:class] = current_class + " " + options[:class]
    link_to(name, options, html_options)
  end
end

and then call it from you navigation partial:

<%= link_to_current_with_class "Index", "current", "/dashboard/index" %>
雪化雨蝶 2024-10-05 14:58:55

我认为您可以使用块参数 link_to_unless_current,它提供当链接是当前页面时要呈现的内容:

<%= link_to_unless_current("Index", :action => 'index') { link_to("Index", {:action => 'index'}, {:class => 'current' }) %>
<%= link_to_unless_current("Account", :action => 'account') { link_to("Account", {:action => 'account'}, {:class => 'current' }) %>
<%= link_to_unless_current("Payments", :action => 'payments') { link_to("Payments", {:action => 'payments'}, {:class => '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:

<%= link_to_unless_current("Index", :action => 'index') { link_to("Index", {:action => 'index'}, {:class => 'current' }) %>
<%= link_to_unless_current("Account", :action => 'account') { link_to("Account", {:action => 'account'}, {:class => 'current' }) %>
<%= link_to_unless_current("Payments", :action => 'payments') { link_to("Payments", {:action => 'payments'}, {:class => 'current' }) %>

Does that work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文