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
本文介绍如何利用RJS技术为表单添加AJAX功能,并演示了更新页面多个元素的方法。通过创建评论的例子,展示了如何实现表单提交后的页面局部刷新。
120

被折叠的 条评论
为什么被折叠?



