small medium large xlarge

Generic-user-small
30 Sep 2010, 19:47
Scott Carleton (2 posts)

I’m new to Jquery and am attempting to use it within my depot application.
I have taken out prototype and replaced it with jquery-1.4.2 along with rails.js.

My line_items_controller.rb has the format.js line in it.

I am having difficulty with the first ajax call in Iteration F2. I am trying to convert the RJS template: create.js.rjs page.replace_html('cart', render(@cart))

with: create.js.erb

$("#cart").html("<%= escape_javascript(render :partial => "cart", :object => @cart) %>");
ajaxLinks();

ajaxLinks is in my application.js:

function ajaxLinks(){
    $('.ajaxForm').submitWithAjax();
    $('a.get').getWithAjax();
    $('a.post').postWithAjax();
    $('a.put').putWithAjax();
    $('a.delete').deleteWithAjax();
}

jQuery.fn.postWithAjax = function() {
  this.unbind('click', false);
  this.click(function() {
    $.post($(this).attr("href"), $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

It’s not rendering and I have no idea why. I think I’m headed in the right direction but am at a loss. Any help would be appreciated.

-Scott

Generic-user-small
01 Oct 2010, 14:46
Scott Carleton (2 posts)

So I got it to work! Scratch everything above. Ryan Bate’s Railscast 136 was extremely helpful.

create.js.erb: $("#cart").html("<%= escape_javascript(render @cart) %>");

jquery:

jQuery.ajaxSetup({
	'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

jQuery.fn.submitWithAjax = function() {
	this.submit(function() {
		$.post($(this).attr("action"), $(this).serialize(), null, "script");
		return false;  
	})
};


$(document).ready(function() {
	$("#add_to_cart").submitWithAjax();
})
You must be logged in to comment