ruby on rails - Javascript voting acts_as_votable -
i have rails app, in posts model has comments , comments votable. i'm using acts_as_votable.
i have voting on comments working. i'm trying implement javascript page not have refresh every time votes on comment, vote goes through.
here had before(which working):
in comments controller:
def upvote_post_comment @post = post.find(params[:post_id]) @comment = @post.comments.find(params[:id]) @comment.liked_by current_user respond_to |format| format.html {redirect_to :back} end end
and in view:
<% if user_signed_in? && current_user != comment.user && !(current_user.voted_for? comment) %> <%= link_to image_tag(‘vote.png'), like_post_comment_path(@post, comment), method: :put %> <a> <%= "#{comment.votes.size}"%></a> <% elsif user_signed_in? && (current_user = comment.user) %> <%= image_tag(‘voted.png')%><a><%= "#{comment.votes.size}"%></a> <% else %> <%= image_tag(‘voted.png')%><a><%= "#{comment.votes.size}"%></a> <% end %>
and have:
in comments controller:
def upvote_post_comment @post = post.find(params[:post_id]) @comment = @post.comments.find(params[:id]) @comment.liked_by current_user respond_to |format| format.html {redirect_to :back } format.json { render json: { count: @comment.liked_count } } end end
and in view:
<% if user_signed_in? && current_user != comment.user && !(current_user.voted_for? comment) %> <%= link_to image_tag(‘vote.png'), like_post_comment_path(@post, comment), method: :put, class: 'vote', remote: true %> <a><%= "#{comment.votes.size}"%></a> <script> $('.vote') .on('ajax:send', function () { $(this).addclass('loading'); }) .on('ajax:complete', function () { $(this).removeclass('loading'); }) .on('ajax:error', function () { $(this).after('<div class="error">there issue.</div>'); }) .on('ajax:success', function (data) { $(this).html(data.count); }); </script> <% elsif user_signed_in? && (current_user = comment.user) %> <%= image_tag(‘voted.png')%><a><%= "#{comment.votes.size}"%></a> <% else %> <%= image_tag(‘voted.png')%><a><%= "#{comment.votes.size}"%></a> <% end %>
this shows me error message: "there issue"
and when refresh page, see vote went through , see in terminal:
started put “/1/comments/1/like" 127.0.0.1 @ 2014-04-06 18:54:38 -0400 processing commentscontroller#upvote_post_comment js parameters: {"post_id"=>”1”, "id"=>”1”}
how voting work via javascript? vote goes through, vote count updates , vote icon updates voted.png instead of vote.png?
your log says request formatted js.
processing commentscontroller#upvote_post_comment js
add data: { type: :json }
link_to
method request json format so,
<%= link_to image_tag('vote.png'), like_post_comment_path(@post, comment), method: :put, class: 'vote', remote: true, data: { type: :json } %>
this tell controller want json response not javascript response.
edit - updates comments.
update controller use,
format.json { render json: { count: @comment.likes.size } }
update js use,
.on('ajax:success', function(e, data, status, xhr) { $(this).html(data.count); });
Comments
Post a Comment