This episode will walk you through adding AJAX functionality to a form using RJS. See how to easily update multiple elements on a page.
<!-- layouts/application.rhtml -->
<%= javascript_include_tag :defaults %>
<!-- products/show.rhtml -->
<% form_remote_for :review, :url => reviews_path, :html => { :id => 'review_form' } do |f| %># reviews_controller.rb
def create
@review = Review.create!(params[:review])
flash[:notice] = "Thank you for reviewing this product"
respond_to do |format|
format.html { redirect_to product_path(@review.product_id) }
format.js
end
end
# create.rjs
page.insert_html :bottom, :reviews, :partial => 'review', :object => @review
page.replace_html :reviews_count, pluralize(@review.product.reviews.size, 'Review')
page[:review_form].reset
page.replace_html :notice, flash[:notice]
flash.discard