# Jquery rails ajax

**URL:** <https://www.ruby-forum.com/t/jquery-rails-ajax/206884>\
**Category:** Rails\
**Created:** [May 12, 2011, 1:48pm UTC](https://www.ruby-forum.com/t/jquery-rails-ajax/206884 "2011-05-12T13:48:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 12, 2011, 1:48pm UTC](https://www.ruby-forum.com/t/jquery-rails-ajax/206884/1 "2011-05-12T13:48:21Z")

</div>

so I am using jquery and updating a dom element with

$(’#thelist’).replaceWith("\<%= escape\_javascript(render(:partial =\>  
@deals)) %\>");

this works fine except it is not hitting the server so if @deals is  
updated, it is not refreshing and showing the latest version. My  
question is how to ajax this thing so that it will update to the  
latest version of @deals. The full code is:  
onPullDown: function () {  
setTimeout(function () {

```
            $('#thelist').replaceWith("<%=

```

escape\_javascript(render(:partial =\> @deals)) %\>");  
myScroll.refresh();  
});  
},

thanks in advance!

---

<div class="post-metadata">

**Author:** ![esdevs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@esdevs](https://www.ruby-forum.com/u/esdevs)\
**Post date:** [May 12, 2011, 3:47pm UTC](https://www.ruby-forum.com/t/jquery-rails-ajax/206884/2 "2011-05-12T15:47:53Z")

</div>

That makes sense. When your page loads, the value of @deals is inserted  
in  
the JavaScript that is sent to the client. As you noticed, since your  
JavaScript code never makes a request back to the server, it just keeps  
calling replaceWith on the value of the original @deals.

The easiest way is probably to have some type of update action that  
responds  
to JS. Then in your view, you can render the newly refreshed @deals.  
You’ll  
also need to update your JavaScript to hit this action on the server and  
update your DOM element with the result. Here are some rough code  
examples.

In your controller:

def some\_update  
@deals = Deal.all  
end

In some\_update.js.erb:

\<%= escape\_javascript(render(:partial =\> @deals)) %\>

Then change your JavaScript to hit some\_update and use the result of  
some\_update:

onPullDown: function () {  
setTimeout(function () {  
.ajax({  
url: ‘controller/some\_update.js’,  
success: function(data) {  
$(’#thelist’).replaceWith(data);  
myScroll.refresh();  
}  
});  
});  
},
