Ruby on rails 通过Rails中的包装器方法传递可选参数

Ruby on rails 通过Rails中的包装器方法传递可选参数,ruby-on-rails,wrapper,link-to,Ruby On Rails,Wrapper,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 =>

我使用以下包装方法将
链接到

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?

多亏了他们的建议,我找到了一个有效的版本:

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

多亏了他们的建议,我找到了一个有效的版本:

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

我认为您不想对这些参数使用splat,我认为最好使用散列(例如
options={}
)。查看一下
链接到
的源代码。我认为您不想对这些参数使用splat,我认为您最好使用散列(例如
选项={}
)。查看
链接到