I've been struggling with creating a simple voting module using AJAX in Rails for the past four days. As someone new to Rails, I'm eager to learn what mistake I might be making!
The website's concept is straightforward - it features a list of posts (referred to as Topics) that users can vote on. I've implemented Devise for user models and Thumbs_up for handling votes.
Here's an excerpt from the view: index.html.erb
<td id='topic_<%=topic.id%>_votes'> <%= pluralize(topic.votes.count, "vote") %> </td>
<td><%= link_to('Vote for this post!', vote_up_topic_path(topic), :method => :post, :remote => true) %></td>
Here's the controller snippet from topics_controller.rb
def vote_up
current_user.vote_exclusively_for(@topic = Topic.find(params[:id]))
respond_to do |format|
format.js
end
end
And here's the JavaScript code found in vote_up.js.erb under views/topics
<% if current_user.voted_on?(@topic = Topic.find(params[:id])) %>
$('topic_<%<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9c4d98897a0a1ab96bfa0bda2">[email protected]</a>%>_votes').html('<%= pluralize(@topic.votes.count, "vote") %>');
<% else %>
alert("You can only vote on <%= @topic.title %> once!")
<% end %>
Despite the fact that the terminal shows that the vote_up.js.erb file is being processed when clicking on the vote link in the Rails server, there is no visible change in the browser. The number of votes only updates upon refreshing the page. While the vote is successfully recorded in the database, there's no immediate confirmation in the browser.
I would appreciate any insights on where I might be going wrong in this setup.