Search code examples
ruby-on-railsajaxruby-on-rails-3unobtrusive-javascriptrails-3-upgrade

link_to_remote attributes/arguments in rails 3


I'm trying to upgade from rails 2.3 to 3.0 and I've found that link_to_remote in rails 2 should be changed to link_to in rails 3 with :remote => true attribute.

And unobtrusive javascript(UJS) for :before, :loading, :failure, :update

But I also have attributes like :url, :href, :title how am I supposed to change that ?

Here is the rails 2.3 code I'm trying to upgrade

 <%= link_to_remote column.label,
  { :url => sort_params,
    :before => "addActiveScaffoldPageToHistory('#{href}', '#{controller_id}')",
    :loading => "Element.addClassName('#{column_header_id}','loading');",
    :failure => "ActiveScaffold.report_500_response('#{active_scaffold_id}')",
    :update => active_scaffold_content_id,
    :method => :get },
  { :href => href ,
   :title => column.header_info}%>

I've analysed lot of websites and Rails documentation but nothing has specified about these attributes for link_to


Solution

  • You can bind callbacks to remote links in Rails 3, the rest of the attributes can be assigned as options.

    link_to column.label,
      sort_params,
      remote: true,
      title: column_header.info,
      id: 'my_remote_link',
      data: {
        href: href,
        controller_id: controller_id,
        column_header_id: column_header_id,
        active_scaffold_id: active_scaffold_id
      }
    

    We'll use the data-attributes for the callbacks.

    $('#my_remote_link').bind('ajax:beforeSend, function() {
      addActiveScaffoldPageToHistory($('#my_remote_link').data('href'), $('#my_remote_link').data('controller_id'));
    });
    

    See http://docs.jquery.com/Ajax_Events for a description of the different ajaxEvents.