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<%= 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?

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'

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