如何扩展 Rails link_to 助手以添加特定类而不破坏 link_to 标准功能。我希望我的自定义助手是这样的:
module MyModule
module MyHelper
def my_cool_link_to(body, url_options, html_options)
# here add 'myclass' to existing classes in html_options
# ?? html_options[:class] || =
link_to body, url_options, html_options
end
end
end
我想保留 link_to 的所有功能:
=my_cool_link_to 'Link title', product_path(@product)
# <a href=".." class="myclass">Link title</a>
=my_cool_link_to 'Link title', product_path(@product), :class=>'btn'
# <a href=".." class="btn myclass">Link title</a>
=my_cool_link_to 'Link title', product_path(@product), :class=>'btn', 'data-id'=>'123'
# <a href=".." data-id="123" class="btn myclass">Link title</a>
等等