<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Module: ActionView::Helpers::PrototypeHelper</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Module</strong></td> <td class="class-name-in-header">ActionView::Helpers::PrototypeHelper</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../../files/lib/action_view/helpers/prototype_helper_rb.html"> lib/action_view/helpers/prototype_helper.rb </a> <br /> </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <p> <a href="http://www.prototypejs.org/">Prototype</a> is a JavaScript library that provides <a href="http://en.wikipedia.org/wiki/Document_Object_Model">DOM</a> manipulation, <a href="http://www.adaptivepath.com/publications/essays/archives/000385.php">Ajax</a> functionality, and more traditional object-oriented facilities for JavaScript. This module provides a set of helpers to make it more convenient to call functions from Prototype using Rails, including functionality to call remote Rails methods (that is, making a background request to a Rails action) using Ajax. This means that you can call actions in your controllers without reloading the page, but still update certain parts of it using injections into the DOM. A common use case is having a form that adds a new element to a list without reloading the page or updating a shopping cart total when a new item is added. </p> <h2>Usage</h2> <p> To be able to use these helpers, you must first include the Prototype JavaScript framework in your pages. </p> <pre> javascript_include_tag 'prototype' </pre> <p> (See the documentation for <a href="JavaScriptHelper.html">ActionView::Helpers::JavaScriptHelper</a> for more information on including this and other JavaScript files in your Rails templates.) </p> <p> Now you‘re ready to call a remote action either through a link… </p> <pre> link_to_remote "Add to cart", :url => { :action => "add", :id => product.id }, :update => { :success => "cart", :failure => "error" } </pre> <p> …through a form… </p> <pre> <% form_remote_tag :url => '/shipping' do -%> <div><%= submit_tag 'Recalculate Shipping' %></div> <% end -%> </pre> <p> …periodically… </p> <pre> periodically_call_remote(:url => 'update', :frequency => '5', :update => 'ticker') </pre> <p> …or through an observer (i.e., a form or field that is observed and calls a remote action when changed). </p> <pre> <%= observe_field(:searchbox, :url => { :action => :live_search }), :frequency => 0.5, :update => :hits, :with => 'query' %> </pre> <p> As you can see, there are numerous ways to use Prototype‘s Ajax functions (and actually more than are listed here); check out the documentation for each method to find out more about its usage and options. </p> <h3>Common Options</h3> <p> See <a href="PrototypeHelper.html#M000455">link_to_remote</a> for documentation of options common to all Ajax helpers; any of the options specified by <a href="PrototypeHelper.html#M000455">link_to_remote</a> can be used by the other helpers. </p> <h2>Designing your Rails actions for Ajax</h2> <p> When building your action handlers (that is, the Rails actions that receive your background requests), it‘s important to remember a few things. First, whatever your action would normally return to the browser, it will return to the Ajax call. As such, you typically don‘t want to render with a layout. This call will cause the layout to be transmitted back to your page, and, if you have a full HTML/CSS, will likely mess a lot of things up. You can turn the layout off on particular actions by doing the following: </p> <pre> class SiteController < ActionController::Base layout "standard", :except => [:ajax_method, :more_ajax, :another_ajax] end </pre> <p> Optionally, you could do this in the method you wish to lack a layout: </p> <pre> render :layout => false </pre> <p> You can tell the type of request from within your action using the <tt>request.xhr?</tt> (XmlHttpRequest, the method that Ajax uses to make background requests) method. </p> <pre> def name # Is this an XmlHttpRequest request? if (request.xhr?) render :text => @name.to_s else # No? Then render an action. render :action => 'view_attribute', :attr => @name end end </pre> <p> The else clause can be left off and the current action will render with full layout and template. An extension to this solution was posted to Ryan Heneise‘s blog at <a href=""http://www.artofmission.com/"">ArtOfMission</a>. </p> <pre> layout proc{ |c| c.request.xhr? ? false : "application" } </pre> <p> Dropping this in your ApplicationController turns the layout off for every request that is an "xhr" request. </p> <p> If you are just returning a little data or don‘t want to build a template for your output, you may opt to simply render text output, like this: </p> <pre> render :text => 'Return this from my method!' </pre> <p> Since whatever the method returns is injected into the DOM, this will simply inject some text (or HTML, if you tell it to). This is usually how small updates, such updating a cart total or a file count, are handled. </p> <h2>Updating multiple elements</h2> <p> See JavaScriptGenerator for information on updating multiple elements on the page in an Ajax response. </p> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000471">build_callbacks</a> <a href="#M000470">build_observer</a> <a href="#M000456">button_to_remote</a> <a href="#M000462">evaluate_remote_response</a> <a href="#M000460">form_remote_for</a> <a href="#M000458">form_remote_tag</a> <a href="#M000455">link_to_remote</a> <a href="#M000469">method_option_to_s</a> <a href="#M000464">observe_field</a> <a href="#M000465">observe_form</a> <a href="#M000468">options_for_ajax</a> <a href="#M000457">periodically_call_remote</a> <a href="#M000459">remote_form_for</a> <a href="#M000463">remote_function</a> <a href="#M000461">submit_to_remote</a> <a href="#M000466">update_page</a> <a href="#M000467">update_page_tag</a> </div> </div> </div> <!-- if includes --> <div id="section"> <div id="constants-list"> <h3 class="section-bar">Constants</h3> <div class="name-list"> <table summary="Constants"> <tr class="top-aligned-row context-row"> <td class="context-item-name">CALLBACKS</td> <td>=</td> <td class="context-item-value">Set.new([ :create, :uninitialized, :loading, :loaded, :interactive, :complete, :failure, :success ] + (100..599).to_a)</td> </tr> <tr class="top-aligned-row context-row"> <td class="context-item-name">AJAX_OPTIONS</td> <td>=</td> <td class="context-item-value">Set.new([ :before, :after, :condition, :url, :asynchronous, :method, :insertion, :position, :form, :with, :update, :script, :type ]).merge(CALLBACKS)</td> </tr> </table> </div> </div> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000456" class="method-detail"> <a name="M000456"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000456.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000456.html');return false;"> <span class="method-name">button_to_remote</span><span class="method-args">(name, options = {}, html_options = {})</span> </a> </div> <div class="method-description"> <p> Creates a button with an onclick event which calls a remote action via XMLHttpRequest The options for specifying the target with :url and defining callbacks is the same as <a href="PrototypeHelper.html#M000455">link_to_remote</a>. </p> </div> </div> <div id="method-M000462" class="method-detail"> <a name="M000462"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000462.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000462.html');return false;"> <span class="method-name">evaluate_remote_response</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns ‘<tt>eval(request.responseText)</tt>’ which is the JavaScript function that <tt><a href="PrototypeHelper.html#M000458">form_remote_tag</a></tt> can call in <tt>:complete</tt> to evaluate a multiple update return document using <tt>update_element_function</tt> calls. </p> </div> </div> <div id="method-M000460" class="method-detail"> <a name="M000460"></a> <div class="method-heading"> <span class="method-name">form_remote_for</span><span class="method-args">(record_or_name_or_array, *args, &proc)</span> </div> <div class="method-description"> <p> Alias for <a href="PrototypeHelper.html#M000459">remote_form_for</a> </p> </div> </div> <div id="method-M000458" class="method-detail"> <a name="M000458"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000458.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000458.html');return false;"> <span class="method-name">form_remote_tag</span><span class="method-args">(options = {}, &block)</span> </a> </div> <div class="method-description"> <p> Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular reloading POST arrangement. Even though it‘s using JavaScript to serialize the form elements, the form submission will work just like a regular submission as viewed by the receiving side (all elements available in <tt>params</tt>). The options for specifying the target with <tt>:url</tt> and defining callbacks is the same as <tt><a href="PrototypeHelper.html#M000455">link_to_remote</a></tt>. </p> <p> A "fall-through" target for browsers that doesn‘t do JavaScript can be specified with the <tt>:action</tt>/<tt>:method</tt> options on <tt>:html</tt>. </p> <p> Example: </p> <pre> # Generates: # <form action="/some/place" method="post" onsubmit="new Ajax.Request('', # {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;"> form_remote_tag :html => { :action => url_for(:controller => "some", :action => "place") } </pre> <p> The Hash passed to the <tt>:html</tt> key is equivalent to the options (2nd) argument in the <a href="FormTagHelper.html#M000532">FormTagHelper.form_tag</a> method. </p> <p> By default the fall-through action is the same as the one specified in the <tt>:url</tt> (and the default method is <tt>:post</tt>). </p> <p> <a href="PrototypeHelper.html#M000458">form_remote_tag</a> also takes a block, like form_tag: </p> <pre> # Generates: # <form action="/" method="post" onsubmit="new Ajax.Request('/', # {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); # return false;"> <div><input name="commit" type="submit" value="Save" /></div> # </form> <% form_remote_tag :url => '/posts' do -%> <div><%= submit_tag 'Save' %></div> <% end -%> </pre> </div> </div> <div id="method-M000455" class="method-detail"> <a name="M000455"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000455.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000455.html');return false;"> <span class="method-name">link_to_remote</span><span class="method-args">(name, options = {}, html_options = nil)</span> </a> </div> <div class="method-description"> <p> Returns a link to a remote action defined by <tt>options[:url]</tt> (using the url_for format) that‘s called in the background using XMLHttpRequest. The result of that request can then be inserted into a DOM object whose id can be specified with <tt>options[:update]</tt>. Usually, the result would be a partial prepared by the controller with render :partial. </p> <p> Examples: </p> <pre> # Generates: <a href="#" onclick="new Ajax.Updater('posts', '/blog/destroy/3', {asynchronous:true, evalScripts:true}); # return false;">Delete this post</a> link_to_remote "Delete this post", :update => "posts", :url => { :action => "destroy", :id => post.id } # Generates: <a href="#" onclick="new Ajax.Updater('emails', '/mail/list_emails', {asynchronous:true, evalScripts:true}); # return false;"><img alt="Refresh" src="/images/refresh.png?" /></a> link_to_remote(image_tag("refresh"), :update => "emails", :url => { :action => "list_emails" }) </pre> <p> You can override the generated HTML options by specifying a hash in <tt>options[:html]</tt>. </p> <pre> link_to_remote "Delete this post", :update => "posts", :url => post_url(@post), :method => :delete, :html => { :class => "destructive" } </pre> <p> You can also specify a hash for <tt>options[:update]</tt> to allow for easy redirection of output to an other DOM element if a server-side error occurs: </p> <p> Example: </p> <pre> # Generates: <a href="#" onclick="new Ajax.Updater({success:'posts',failure:'error'}, '/blog/destroy/5', # {asynchronous:true, evalScripts:true}); return false;">Delete this post</a> link_to_remote "Delete this post", :url => { :action => "destroy", :id => post.id }, :update => { :success => "posts", :failure => "error" } </pre> <p> Optionally, you can use the <tt>options[:position]</tt> parameter to influence how the target DOM element is updated. It must be one of <tt>:before</tt>, <tt>:top</tt>, <tt>:bottom</tt>, or <tt>:after</tt>. </p> <p> The method used is by default POST. You can also specify GET or you can simulate PUT or DELETE over POST. All specified with <tt>options[:method]</tt> </p> <p> Example: </p> <pre> # Generates: <a href="#" onclick="new Ajax.Request('/person/4', {asynchronous:true, evalScripts:true, method:'delete'}); # return false;">Destroy</a> link_to_remote "Destroy", :url => person_url(:id => person), :method => :delete </pre> <p> By default, these remote requests are processed asynchronous during which various JavaScript callbacks can be triggered (for progress indicators and the likes). All callbacks get access to the <tt>request</tt> object, which holds the underlying XMLHttpRequest. </p> <p> To access the server response, use <tt>request.responseText</tt>, to find out the HTTP status, use <tt>request.status</tt>. </p> <p> Example: </p> <pre> # Generates: <a href="#" onclick="new Ajax.Request('/words/undo?n=33', {asynchronous:true, evalScripts:true, # onComplete:function(request){undoRequestCompleted(request)}}); return false;">hello</a> word = 'hello' link_to_remote word, :url => { :action => "undo", :n => word_counter }, :complete => "undoRequestCompleted(request)" </pre> <p> The callbacks that may be specified are (in order): </p> <table> <tr><td valign="top"><tt>:loading</tt>:</td><td>Called when the remote document is being loaded with data by the browser. </td></tr> <tr><td valign="top"><tt>:loaded</tt>:</td><td>Called when the browser has finished loading the remote document. </td></tr> <tr><td valign="top"><tt>:interactive</tt>:</td><td>Called when the user can interact with the remote document, even though it has not finished loading. </td></tr> <tr><td valign="top"><tt>:success</tt>:</td><td>Called when the XMLHttpRequest is completed, and the HTTP status code is in the 2XX range. </td></tr> <tr><td valign="top"><tt>:failure</tt>:</td><td>Called when the XMLHttpRequest is completed, and the HTTP status code is not in the 2XX range. </td></tr> <tr><td valign="top"><tt>:complete</tt>:</td><td>Called when the XMLHttpRequest is complete (fires after success/failure if they are present). </td></tr> </table> <p> You can further refine <tt>:success</tt> and <tt>:failure</tt> by adding additional callbacks for specific status codes. </p> <p> Example: </p> <pre> # Generates: <a href="#" onclick="new Ajax.Request('/testing/action', {asynchronous:true, evalScripts:true, # on404:function(request){alert('Not found...? Wrong URL...?')}, # onFailure:function(request){alert('HTTP Error ' + request.status + '!')}}); return false;">hello</a> link_to_remote word, :url => { :action => "action" }, 404 => "alert('Not found...? Wrong URL...?')", :failure => "alert('HTTP Error ' + request.status + '!')" </pre> <p> A status code callback overrides the success/failure handlers if present. </p> <p> If you for some reason or another need synchronous processing (that‘ll block the browser while the request is happening), you can specify <tt>options[:type] = :synchronous</tt>. </p> <p> You can customize further browser side call logic by passing in JavaScript code snippets via some optional parameters. In their order of use these are: </p> <table> <tr><td valign="top"><tt>:confirm</tt>:</td><td>Adds confirmation dialog. </td></tr> <tr><td valign="top"><tt>:condition</tt>:</td><td>Perform remote request conditionally by this expression. Use this to describe browser-side conditions when request should not be initiated. </td></tr> <tr><td valign="top"><tt>:before</tt>:</td><td>Called before request is initiated. </td></tr> <tr><td valign="top"><tt>:after</tt>:</td><td>Called immediately after request was initiated and before <tt>:loading</tt>. </td></tr> <tr><td valign="top"><tt>:submit</tt>:</td><td>Specifies the DOM element ID that‘s used as the parent of the form elements. By default this is the current form, but it could just as well be the ID of a table row or any other DOM element. </td></tr> <tr><td valign="top"><tt>:with</tt>:</td><td>A JavaScript expression specifying the parameters for the XMLHttpRequest. Any expressions should return a valid URL query string. <p> Example: </p> <pre> :with => "'name=' + $('name').value" </pre> </td></tr> </table> <p> You can generate a link that uses AJAX in the general case, while degrading gracefully to plain link behavior in the absence of JavaScript by setting <tt>html_options[:href]</tt> to an alternate URL. Note the extra curly braces around the <tt>options</tt> hash separate it as the second parameter from <tt>html_options</tt>, the third. </p> <p> Example: </p> <pre> link_to_remote "Delete this post", { :update => "posts", :url => { :action => "destroy", :id => post.id } }, :href => url_for(:action => "destroy", :id => post.id) </pre> </div> </div> <div id="method-M000464" class="method-detail"> <a name="M000464"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000464.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000464.html');return false;"> <span class="method-name">observe_field</span><span class="method-args">(field_id, options = {})</span> </a> </div> <div class="method-description"> <p> Observes the field with the DOM ID specified by <tt>field_id</tt> and calls a callback when its contents have changed. The default callback is an Ajax call. By default the value of the observed field is sent as a parameter with the Ajax call. </p> <p> Example: </p> <pre> # Generates: new Form.Element.Observer('suggest', 0.25, function(element, value) {new Ajax.Updater('suggest', # '/testing/find_suggestion', {asynchronous:true, evalScripts:true, parameters:'q=' + value})}) <%= observe_field :suggest, :url => { :action => :find_suggestion }, :frequency => 0.25, :update => :suggest, :with => 'q' %> </pre> <p> Required <tt>options</tt> are either of: </p> <table> <tr><td valign="top"><tt>:url</tt>:</td><td><tt>url_for</tt>-style options for the action to call when the field has changed. </td></tr> <tr><td valign="top"><tt>:function</tt>:</td><td>Instead of making a remote call to a URL, you can specify javascript code to be called instead. Note that the value of this option is used as the <b>body</b> of the javascript function, a function definition with parameters named element and value will be generated for you for example: <pre> observe_field("glass", :frequency => 1, :function => "alert('Element changed')") </pre> <p> will generate: </p> <pre> new Form.Element.Observer('glass', 1, function(element, value) {alert('Element changed')}) </pre> <p> The element parameter is the DOM element being observed, and the value is its value at the time the observer is triggered. </p> </td></tr> </table> <p> Additional options are: </p> <table> <tr><td valign="top"><tt>:frequency</tt>:</td><td>The frequency (in seconds) at which changes to this field will be detected. Not setting this option at all or to a value equal to or less than zero will use event based observation instead of time based observation. </td></tr> <tr><td valign="top"><tt>:update</tt>:</td><td>Specifies the DOM ID of the element whose innerHTML should be updated with the XMLHttpRequest response text. </td></tr> <tr><td valign="top"><tt>:with</tt>:</td><td>A JavaScript expression specifying the parameters for the XMLHttpRequest. The default is to send the key and value of the observed field. Any custom expressions should return a valid URL query string. The value of the field is stored in the JavaScript variable <tt>value</tt>. <p> Examples </p> <pre> :with => "'my_custom_key=' + value" :with => "'person[name]=' + prompt('New name')" :with => "Form.Element.serialize('other-field')" </pre> <p> Finally </p> <pre> :with => 'name' </pre> <p> is shorthand for </p> <pre> :with => "'name=' + value" </pre> <p> This essentially just changes the key of the parameter. </p> </td></tr> </table> <p> Additionally, you may specify any of the options documented in the <em>Common options</em> section at the top of this document. </p> <p> Example: </p> <pre> # Sends params: {:title => 'Title of the book'} when the book_title input # field is changed. observe_field 'book_title', :url => 'http://example.com/books/edit/1', :with => 'title' </pre> </div> </div> <div id="method-M000465" class="method-detail"> <a name="M000465"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000465.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000465.html');return false;"> <span class="method-name">observe_form</span><span class="method-args">(form_id, options = {})</span> </a> </div> <div class="method-description"> <p> Observes the form with the DOM ID specified by <tt>form_id</tt> and calls a callback when its contents have changed. The default callback is an Ajax call. By default all fields of the observed field are sent as parameters with the Ajax call. </p> <p> The <tt>options</tt> for <tt><a href="PrototypeHelper.html#M000465">observe_form</a></tt> are the same as the options for <tt><a href="PrototypeHelper.html#M000464">observe_field</a></tt>. The JavaScript variable <tt>value</tt> available to the <tt>:with</tt> option is set to the serialized form by default. </p> </div> </div> <div id="method-M000457" class="method-detail"> <a name="M000457"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000457.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000457.html');return false;"> <span class="method-name">periodically_call_remote</span><span class="method-args">(options = {})</span> </a> </div> <div class="method-description"> <p> Periodically calls the specified url (<tt>options[:url]</tt>) every <tt>options[:frequency]</tt> seconds (default is 10). Usually used to update a specified div (<tt>options[:update]</tt>) with the results of the remote call. The options for specifying the target with <tt>:url</tt> and defining callbacks is the same as <a href="PrototypeHelper.html#M000455">link_to_remote</a>. Examples: </p> <pre> # Call get_averages and put its results in 'avg' every 10 seconds # Generates: # new PeriodicalExecuter(function() {new Ajax.Updater('avg', '/grades/get_averages', # {asynchronous:true, evalScripts:true})}, 10) periodically_call_remote(:url => { :action => 'get_averages' }, :update => 'avg') # Call invoice every 10 seconds with the id of the customer # If it succeeds, update the invoice DIV; if it fails, update the error DIV # Generates: # new PeriodicalExecuter(function() {new Ajax.Updater({success:'invoice',failure:'error'}, # '/testing/invoice/16', {asynchronous:true, evalScripts:true})}, 10) periodically_call_remote(:url => { :action => 'invoice', :id => customer.id }, :update => { :success => "invoice", :failure => "error" } # Call update every 20 seconds and update the new_block DIV # Generates: # new PeriodicalExecuter(function() {new Ajax.Updater('news_block', 'update', {asynchronous:true, evalScripts:true})}, 20) periodically_call_remote(:url => 'update', :frequency => '20', :update => 'news_block') </pre> </div> </div> <div id="method-M000459" class="method-detail"> <a name="M000459"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000459.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000459.html');return false;"> <span class="method-name">remote_form_for</span><span class="method-args">(record_or_name_or_array, *args, &proc)</span> </a> </div> <div class="method-description"> <p> 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. </p> <h3>Resource</h3> <p> Example: </p> <pre> <% remote_form_for(@post) do |f| %> ... <% end %> </pre> <p> This will expand to be the same as: </p> <pre> <% remote_form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %> ... <% end %> </pre> <h3>Nested Resource</h3> <p> Example: </p> <pre> <% remote_form_for([@post, @comment]) do |f| %> ... <% end %> </pre> <p> This will expand to be the same as: </p> <pre> <% remote_form_for :comment, @comment, :url => post_comment_path(@post, @comment), :html => { :method => :put, :class => "edit_comment", :id => "edit_comment_45" } do |f| %> ... <% end %> </pre> <p> If you don‘t need to attach a form to a resource, then check out <a href="PrototypeHelper.html#M000458">form_remote_tag</a>. </p> <p> See <a href="FormHelper.html#M000565">FormHelper#form_for</a> for additional semantics. </p> </div> </div> <div id="method-M000463" class="method-detail"> <a name="M000463"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000463.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000463.html');return false;"> <span class="method-name">remote_function</span><span class="method-args">(options)</span> </a> </div> <div class="method-description"> <p> Returns the JavaScript needed for a remote function. Takes the same arguments as <a href="PrototypeHelper.html#M000455">link_to_remote</a>. </p> <p> Example: </p> <pre> # Generates: <select id="options" onchange="new Ajax.Updater('options', # '/testing/update_options', {asynchronous:true, evalScripts:true})"> <select id="options" onchange="<%= remote_function(:update => "options", :url => { :action => :update_options }) %>"> <option value="0">Hello</option> <option value="1">World</option> </select> </pre> </div> </div> <div id="method-M000461" class="method-detail"> <a name="M000461"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000461.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000461.html');return false;"> <span class="method-name">submit_to_remote</span><span class="method-args">(name, value, options = {})</span> </a> </div> <div class="method-description"> <p> Returns a button input tag with the element name of <tt>name</tt> and a value (i.e., display text) of <tt>value</tt> that will submit form using XMLHttpRequest in the background instead of a regular POST request that reloads the page. </p> <pre> # Create a button that submits to the create action # # Generates: <input name="create_btn" onclick="new Ajax.Request('/testing/create', # {asynchronous:true, evalScripts:true, parameters:Form.serialize(this.form)}); # return false;" type="button" value="Create" /> <%= submit_to_remote 'create_btn', 'Create', :url => { :action => 'create' } %> # Submit to the remote action update and update the DIV succeed or fail based # on the success or failure of the request # # Generates: <input name="update_btn" onclick="new Ajax.Updater({success:'succeed',failure:'fail'}, # '/testing/update', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this.form)}); # return false;" type="button" value="Update" /> <%= submit_to_remote 'update_btn', 'Update', :url => { :action => 'update' }, :update => { :success => "succeed", :failure => "fail" } </pre> <p> <tt>options</tt> argument is the same as in <a href="PrototypeHelper.html#M000458">form_remote_tag</a>. </p> </div> </div> <div id="method-M000466" class="method-detail"> <a name="M000466"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000466.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000466.html');return false;"> <span class="method-name">update_page</span><span class="method-args">(&block)</span> </a> </div> <div class="method-description"> <p> Yields a JavaScriptGenerator and returns the generated JavaScript code. Use this to update multiple elements on a page in an Ajax response. See JavaScriptGenerator for more information. </p> <p> Example: </p> <pre> update_page do |page| page.hide 'spinner' end </pre> </div> </div> <div id="method-M000467" class="method-detail"> <a name="M000467"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000467.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000467.html');return false;"> <span class="method-name">update_page_tag</span><span class="method-args">(html_options = {}, &block)</span> </a> </div> <div class="method-description"> <p> Works like <a href="PrototypeHelper.html#M000466">update_page</a> but wraps the generated JavaScript in a <script> tag. Use this to include generated JavaScript in an ERb template. See JavaScriptGenerator for more information. </p> <p> <tt>html_options</tt> may be a hash of <script> attributes to be passed to <a href="JavaScriptHelper.html#M000514">ActionView::Helpers::JavaScriptHelper#javascript_tag</a>. </p> </div> </div> <h3 class="section-bar">Protected Instance methods</h3> <div id="method-M000471" class="method-detail"> <a name="M000471"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000471.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000471.html');return false;"> <span class="method-name">build_callbacks</span><span class="method-args">(options)</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000470" class="method-detail"> <a name="M000470"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000470.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000470.html');return false;"> <span class="method-name">build_observer</span><span class="method-args">(klass, name, options = {})</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000469" class="method-detail"> <a name="M000469"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000469.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000469.html');return false;"> <span class="method-name">method_option_to_s</span><span class="method-args">(method)</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000468" class="method-detail"> <a name="M000468"></a> <div class="method-heading"> <a href="PrototypeHelper.src/M000468.html" target="Code" class="method-signature" onclick="popupCode('PrototypeHelper.src/M000468.html');return false;"> <span class="method-name">options_for_ajax</span><span class="method-args">(options)</span> </a> </div> <div class="method-description"> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>