# No update after AJAX request!

**URL:** <https://www.ruby-forum.com/t/no-update-after-ajax-request/133138>\
**Category:** Rails\
**Created:** [March 28, 2008, 7:32am UTC](https://www.ruby-forum.com/t/no-update-after-ajax-request/133138 "2008-03-28T07:32:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![niedral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed655f/32.png) [@niedral](https://www.ruby-forum.com/u/niedral)\
**Post date:** [March 28, 2008, 7:32am UTC](https://www.ruby-forum.com/t/no-update-after-ajax-request/133138/1 "2008-03-28T07:32:57Z")

</div>

I try to implement a little AJAX powered voting on a site.

I checked, that the template includes the javascript libraries:

\<%= javascript\_include\_tag :defaults %\>

and add a remote form to the template:

```
<div id="voting<%= artist.id %>">
  <%= render(:partial => "voting", :object => artist) %>
</div>

```

The partial template \_voting.rhtml does exist:

Votings: \<%= voting.votes %\>  
\<% form\_remote\_tag :url =\> { :action =\> :vote, :id =\> voting } do %\>  
\<%= submit\_tag “Vote” %\>  
\<% end %\>

In the controller I implemented the method to handle the request:

def vote  
begin  
@artist = Artist.find(params[:id])  
rescue ActiveRecord::RecordNotFound  
logger.error(“Attempt to access invalid artist #{params[:id]}”)  
else  
@artist.votes = @artist.votes + 1  
@artist.save  
end  
end

and there is an RJS template vote.rjs:

page.replace\_html(“voting” + @artist.id.to\_s, :partial =\>  
“voting”, :object =\> @artist)

When I call the page and press the vote button, the request is send to  
the server and the database was updated, but the website did not  
change. When I reload the page the correct new value will be  
displayed.

I debugged this with FireBug in Firefox an I can see the ajax request  
thats been sent to the server and the following response to this  
request:

try {  
Element.update(“voting215”, “Votings: 26\n\<form action=”/voting/vote/  
215" method=“post” onsubmit  
=“new Ajax.Request(’/voting/vote/215’, {asynchronous:true,  
evalScripts:true, parameters:Form.serialize  
(this)}); return false;”\> \n\t\<input name=“commit” type=“submit”  
value=“Vote” /\> \n\n"  
);  
} catch (e) { alert(‘RJS error:\n\n’ + e.toString());  
alert(‘Element.update(“voting215”, “Votings  
: 26\n\<form action=\”/voting/vote/215\" method=\“post\”  
onsubmit=\"new Ajax.Request(’/voting  
/vote/215’, {asynchronous:true, evalScripts:true,  
parameters:Form.serialize(this)}); return false;  
\"\> \n\t\<input name=\“commit\” type=\“submit\” value=\  
“Vote\” /\> \n\n");’);  
throw e }

Does anyone has an idea what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![niedral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed655f/32.png) [@niedral](https://www.ruby-forum.com/u/niedral)\
**Post date:** [March 28, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/no-update-after-ajax-request/133138/2 "2008-03-28T21:21:13Z")

</div>

I found the solution by myself! It was a wrong Content-type of the  
AJAX response.

I don’t know why, but the Content-type of the response was text/html  
instead of text/javascript an so the response was not evaluated by  
prototype.  
If fixed this by explicitly setting the content type in the action  
using:

```
 @headers['Content-Type'] = 'text/javascript; charset=utf-8'
```
