2

Rails の link_to 関数にいくつかの機能を追加する関数を作成しようとしています。私がやりたいことは、単にクラスを追加することです。私がこれまでに持っているもの:

#application_helper.rb
def button_link(*args)
    link_to(*args.push(class: 'btn'))
end

問題は、別のクラスを button_link 関数に追加すると機能しないことです。

例:

<td class='button'>
    <%= button_link "Show", category_path(item), class: "btn-primary" %>
</td>

次のエラーが表示されますwrong number of arguments (4 for 3)。どうすればこれを正しく行うことができますか?

4

2 に答える 2

1

link_to フォームを維持するために、ヘルパー メソッドを次のように変更してみてください。

def button_link(name, url_options, html_options = {})
  if html_options.has_key?(:class)
    css_options = html_options.fetch(:class)
    css_options << ' current'

    html_options.merge!( { :class => css_options } )
  else
    html_options.merge!( { :class => ' btn' } )
  end

  link_to(name, url_options, html_options)
end
于 2013-06-04T14:52:33.140 に答える