Rails 3 - 自定义 link_to 帮助器(具有默认类和添加类的能力)
我正在尝试连接一个自定义助手,该助手具有默认类“pjax”,但也保留在需要时添加类的能力。
示例:
link_to_pjax('pagename', page_path, :class => 'current')
所以助手会默认添加“pjax”,以及类“current”,或者传入的任何内容。
def link_to_pjax(name, path, options = {:class => 'pjax'})
link_to(name, path, options)
end
语法让我感到害怕。任何建议将不胜感激。
I'm trying to hook up a custom helper that has a default class 'pjax' but also retains an ability to add classes where need be.
Example:
link_to_pjax('pagename', page_path, :class => 'current')
So the helper would add the 'pjax' by default, and also the class 'current', or whatever is passed in.
def link_to_pjax(name, path, options = {:class => 'pjax'})
link_to(name, path, options)
end
The syntax is freaking me out. Any advice would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑
经过测试,它不太优雅:
我的第一个解决方案有效,但前提是您仍然指定了一个类。
最新的适用于所有情况:
我的坏...
edit
After test, it's much less elegant:
My first solution works but only if you have still specified a class.
The latest works in all cases:
My bad...
我改进了 Delba 答案来处理 link_to 的块版本:
I improved Delba answer to handle block version of link_to: