method

remote_form_for

rails latest stable - Class: ActionView::Helpers::PrototypeHelper

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v2.3.8) is shown here.

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.

5Notes

Update element after remote call

Vidmantas · Aug 13, 20087 thanks

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 %>

If you're not using resource

Vidmantas · Sep 30, 20085 thanks

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 ... %>

Using a Loading Graphic

pol · Oct 21, 20085 thanks

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.

Re: Using a Loading Graphic

mcmire · Nov 7, 20083 thanks

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

Common AJAX options

foliosus · Jan 18, 20112 thanks

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