Railsのラッパーメソッドを使用してオプションの引数を渡します
-
19-09-2019 - |
質問
私は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
所属していません StackOverflow