No update after AJAX request!

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<%= %>">
  <%= render(:partial => "voting", :object => artist) %>

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
@artist = Artist.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error(“Attempt to access invalid artist #{params[:id]}”)
@artist.votes = @artist.votes + 1

and there is an RJS template vote.rjs:

page.replace_html(“voting” +, :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

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

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?

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
If fixed this by explicitly setting the content type in the action

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs