我有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显示?

有帮助吗?

解决方案

由于theIV的建议下,我发现,工作的版本:

def link_to_with_current(text, link, condition, *args)
  options = args.first || {}
  options[:class] = condition ? 'current' : nil
  link_to text, link, options
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top