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>
等