Skip to content

Adding AJAX to forms

Ronald Chan edited this page Feb 26, 2012 · 2 revisions

This guide assumes you already have site-wide navigation set up.

Connecting your form up to AJAX Pagination is just as simple as with your links.

Simply change your current form_tag calls:

<%= form_tag posts_url, :method => "post", :class => "myclass" do %>
 ...
<% end %>

to use ajax_form_tag, and specify which section your new content should be displayed in using the :pagination option:

<%= ajax_form_tag posts_url, :method => "post", :class => "myclass", :pagination => "page" do %>
 ...
<% end %>

Or if you are using form_for:

<%= form_for @post, :method => "post", :html => {:class => "myclass"} do %>
 ...
<% end %>

Then use ajax_form_for instead

<%= ajax_form_for @post, :method => "post", :html => {:class => "myclass", :pagination => "menu"} do %>
 ...
<% end %>

If you followed our advice in the site navigation guide, you can actually omit the pagination option, because the default is the pagination section named the empty string "".

For more on AJAX functionality in forms, see Creating AJAX forms including with other form helpers.