Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3.0.9 Ajax response not executed
    primarykey
    data
    text
    <p>I'm using Rails 3.0.9 and jQuery 1.6.2 with jQuery-ujs.</p> <p>Basically I'd like to ajaxfy my "add to cart" action.</p> <p>In my view:</p> <pre><code>&lt;td&gt;&lt;%= button_to "Add to cart", line_items_path(:coupon_id =&gt; coupon, :format =&gt; :js), :remote =&gt; true %&gt; </code></pre> <p>In my controller:</p> <pre><code>def create @cart = current_cart coupon = Coupon.find(params[:coupon_id]) @line_item = @cart.add_coupon(coupon.id) respond_to do |format| if @line_item.save format.html { redirect_to(@line_item.cart) } format.js {render :layout =&gt; false, :content_type =&gt; 'text/javascript'} format.xml { render :xml =&gt; @line_item, :status =&gt; :created, :location =&gt; @line_item } else format.html { render :action =&gt; "new" } format.js {render :layout =&gt; false, :content_type =&gt; 'text/javascript'} format.xml { render :xml =&gt; @line_item.errors, :status =&gt; :unprocessable_entity } end end </code></pre> <p>end</p> <p>I've also created a create.js.erb:</p> <pre><code>$("#my_cart").html("&lt;%= escape_javascript(render(:partial =&gt; 'layouts/mycart')) %&gt;"); </code></pre> <p>to update the cart item number whenever a new item is added.</p> <p>When I click the "add to cart" button, a JS POST request is sent to the server:</p> <pre><code> Started POST "/line_items.js?coupon_id=1" for 127.0.0.1 at Thu Aug 18 00:04:31 +0800 2011 Processing by LineItemsController#create as JS </code></pre> <p>And the response from the server is:</p> <pre><code>Rendered layouts/_mycart.html.erb (5.3ms) Rendered line_items/create.js.erb (28.9ms) Completed 200 OK in 127ms (Views: 58.7ms | ActiveRecord: 3.0ms) </code></pre> <p>The response's body contains the javascript code to update my cart:</p> <pre><code>$("#my_cart").html("&lt;li id=\"my_cart\"&gt;&lt;a href=\"/carts/10\"&gt;My cart(1)&lt;\/a&gt;\n&lt;\/li&gt;"); </code></pre> <p>However, the web page is not updated. I tried different browsers (Chrome, Firefox, Safari). But if I manually run this code in Firebug's console, the page got updated. I've googled for a day and tried different solutions. None of them worked me. Could someone solve my problem?</p> <p>Here's the code generated for button_to:</p> <pre><code>&lt;form method="post" action="/line_items.js?coupon_id=1" data-remote="true" class="button_to"&gt;&lt;div&gt;&lt;input type="submit" value="Add to cart" /&gt;&lt;input name="authenticity_token" type="hidden" value="MdHScf0JVNu2UFXLxuindDvZ5TPBztfMMzv57w1LWX8=" /&gt;&lt;/div&gt;&lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload