RailsCasts Pro episodes are now free!
Learn more or hide this
There is a newer version of this episode, see the revised episode.
Resources
<%= javascript_include_tag 'jquery', 'application' %>
jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} }) jQuery.fn.submitWithAjax = function() { this.submit(function() { $.post(this.action, $(this).serialize(), null, "script"); return false; }) return this; }; $(document).ready(function() { $("#new_review").submitWithAjax(); })
$("#new_review").before('<div id="flash_notice"><%= escape_javascript(flash.delete(:notice)) %></div>'); $("#reviews_count").html("<%= pluralize(@review.product.reviews.count, 'Review') %>"); $("#reviews").append("<%= escape_javascript(render(:partial => @review)) %>"); $("#new_review")[0].reset();
def create @review = Review.create!(params[:review]) flash[:notice] = "Thank you for reviewing this product" respond_to do |format| format.html { redirect_to @review.product } format.js end end