Hello a rails 4 newbie here. I cant seem to get ajax to update the view. I am at a loss. I can get an alert to pop up when click the +1 link in the view but I can't get the view to update (show the votes) the span with id="ajaxed", even though the database is updating. I have read the rails guides on ajax and look at about 6 videos on youtube on how to do this and it seems right to me but doesn't work for me. here is my html:
<tbody>
<% @topics.each do |topic| %>
<tr>
<td><%= topic.title %></td>
<td><%= topic.description %></td>
<td><span id="ajaxed"><%=pluralize(topic.votes.count, "vote")%></span></td>
<td><%= link_to "+1", upvote_topic_path(topic), id: "upvote", remote: true, method: :post%></td>
<td><%= pluralize(topic.downvotes.count, "downvote") %></td>
<td><%= link_to "-1", downvote_topic_path(topic), method: :post %></td>
<td><%= link_to 'Show', topic %></td>
<td><%= link_to 'Edit', edit_topic_path(topic) %></td>
<td><%= link_to 'Destroy', topic, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Here is my controller:
def upvote
@topic = Topic.find(params[:id])
@topic.votes.create
#redirect_to topics_path
respond_to do |format|
format.html { redirect_to topics_path }
format.js
end
end
here is my upvote.js.erb:
$('#ajaxed').text('<%=pluralize(topic.votes.count, "vote")%>');
BTW any other form of javascript or jquery that I do on the upvote.js.erb seems to work, it just wont update my '#ajaxed' span. I also tried .html and even straight js document.getElementById("ajaxed").innerHTML = '<%=pluralize(topic.votes.count, "vote")%>', this gives me unwanted results. Any help would be greatly appreciated. Thanks!!!
Aucun commentaire:
Enregistrer un commentaire