link_to_remote と form_remote_tag にHTMLオプションを渡す

RailsAjaxを利用するためのヘルパーメソッドとしてlink_to_remoteやform_remote_tagは良く利用するけど、出力される<a>タグにclassやstyle等のHTMLオプションを付け加えたい。Ruby on Rails APIには記述はないけれど、ソースを眺めてみたら指定できるみたい。


link_to_remote(name, options = {}, html_options = {})

まずはlink_to_remoteだが、3番目の引数名からも想像できる通りだった。

<%= link_to_remote(
  '表示される文字列',
  {
    :url => {:controller => 'sample', :action => 'test'},
    :update => 'target_to_update'
  },
  {
    :class => 'sample_class',
    :style => 'margin: 10px; padding: 10px;'
  }
) %>

このように指定した結果、以下の出力が成される。見やすいように改行は手動でいれている。

<a class="sample_class" href="#" style="margin: 10px; padding: 10px;"
  onclick="new Ajax.Updater(
    'target_to_update',
    '/sample/test',
    {
      asynchronous:true,
      evalScripts:true,
    }
  ); return false;">表示される文字列</a>
form_remote_tag(options = {})

続いてform_remote_tagだが、見るからにくせもの。引数1つってなに。。。

<%= form_remote_tag(
  :url => {:controller => 'sample', :action => 'test'},
  :update => 'target_to_update',
  :html =>
  {
    :class => 'sample_class',
    :style => 'margin: 10px; padding: 10px;'
  }
) %>

このように指定した結果、以下の出力が成される。

<form action="/sample/test" class="sample_class"
  method="post" style="margin: 10px; padding: 10px;"
  onsubmit="new Ajax.Updater(
    'target_to_update',
    '/sample/test',
    {
      asynchronous:true,
      evalScripts:true
    },
    parameters:Form.serialize(this)}
  ); return false;">


どちらにおいても指定できることはわかって良かったけれど、できれば統一してほしかった。