Flowdock
method

remote_form_for

Importance_5
v2.0.3 - Show latest stable - 5 notes - Class: ActionView::Helpers::PrototypeHelper
remote_form_for(record_or_name_or_array, *args, &proc) public

Creates a form that will submit using XMLHttpRequest in the background instead of the regular reloading POST arrangement and a scope around a specific resource that is used as a base for questioning about values for the fields.

Resource

Example:

  <% remote_form_for(@post) do |f| %>
    ...
  <% end %>

This will expand to be the same as:

  <% remote_form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %>
    ...
  <% end %>

Nested Resource

Example:

  <% remote_form_for([@post, @comment]) do |f| %>
    ...
  <% end %>

This will expand to be the same as:

  <% remote_form_for :comment, @comment, :url => post_comment_path(@post, @comment), :html => { :method => :put, :class => "edit_comment", :id => "edit_comment_45" } do |f| %>
    ...
  <% end %>

If you don’t need to attach a form to a resource, then check out form_remote_tag.

See FormHelper#form_for for additional semantics.

Show source
Register or log in to add new notes.
August 13, 2008
7 thanks

Update element after remote call

Not mentioned in the documentation, you can add :update option to the remote_form_for and pass the id of element you’d like to update after ajax action as you do with link_to_remote, for example:

<% remote_form_for "comment", :update => "form" } do |f| %>
  # your form here
<% end %>

Or

<% remote_form_for "comment", :update => {:success => "form", :failure => "errors"} do |f| %>
  # your form here
<% end %>
October 21, 2008
5 thanks

Using a Loading Graphic

If you want to make a little loading graphic, typically you use an animated gif (like a little spinner or something). Both link_to_remote and remote_form_for allow you to easily do this by using the :loaded and :loading triggers to call javascript.

For example:

<% remote_form_for @survey, :loading => "$('loading').show();", :loaded => "$('loading').hide();" do |f| %>
  <%= submit_tag ' Save' %>
  <%= image_tag "indicator_circle.gif", :style => 'display: none;', :id => 'loading' %>
<% end %>

The ‘loading’ parameter used for the ‘$’ prototype selector is the id of the animated gif. It starts out hidden, and is toggled by the loading/loaded triggers.

September 30, 2008
5 thanks

If you're not using resource

If you don’t use resource for your remote_form_for, then :url option is necessary.

For example:

<% remote_form_for "not_resource" do |f| ... %> 

won’t work. But with :url option, it will:

<% remote_form_for "not_resource", 
     :url => { :controller => "recommend", :action => "send" } do ... %> 
November 7, 2008
3 thanks

Re: Using a Loading Graphic

You probably want to be using :complete, not :loaded, to execute Javascript when an Ajax request has finished. See: http://prototypejs.org/api/ajax/options

January 18, 2011
2 thanks

Common AJAX options

See the documentation for link_to_remote to see the common AJAX options, like :before and :completed.