通过 Rails 中的包装方法传递可选参数

发布于 2024-08-20 18:30:41 字数 462 浏览 4 评论 0原文

我有以下 link_to 包装方法:

def link_to_with_current(text, link, condition, *args)
  current_class = condition ? 'current' : nil
  link_to text, link, :class => current_class, *args
end

使用此示例调用时:

link_to_with_current 'My Link', '/mylink.html', true, :id => 'mylink'

生成以下链接:

<a href="/mylink" class="current">My Link</a>

为什么 ID 不显示?

I have the following wrapper method for link_to:

def link_to_with_current(text, link, condition, *args)
  current_class = condition ? 'current' : nil
  link_to text, link, :class => current_class, *args
end

When called with this sample:

link_to_with_current 'My Link', '/mylink.html', true, :id => 'mylink'

The following link is generated:

<a href="/mylink" class="current">My Link</a>

Why doesn't the ID show up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

牵强ㄟ 2024-08-27 18:30:41

感谢 theIV 的建议,我找到了一个可行的版本:

def link_to_with_current(text, link, condition, *args)
  options = args.first || {}
  options[:class] = condition ? 'current' : nil
  link_to text, link, options
end

Thanks to theIV's suggestion, I found a version that works:

def link_to_with_current(text, link, condition, *args)
  options = args.first || {}
  options[:class] = condition ? 'current' : nil
  link_to text, link, options
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文