Ich benutze acts_as_votable für mein Abstimmungssystem in Ruby. Im Moment kann ich den Zähler abstimmen und den Zähler aktualisieren. Aber was ich will, ist zu wählen, und dann zum Beispiel runterstimmen und abstimmen, aber das kann ich nur mit erfrischend machen. Und wenn ich abstimmen und dann ablehnen, wie kann ich den Stimmzähler auf "-1" aktualisieren?ruby on rails: vote/unvoote mit ajax
Controller:
def upvote
@improvement_action.upvote_from current_user #, :vote_scope => 'upvote'
respond_to do |format|
format.json { render json: { count: @improvement_action.get_upvotes.size } }
format.html {redirect_to :back}
end
end
def downvote
@improvement_action.downvote_from current_user#, :vote_scope => 'upvote'
respond_to do |format|
format.json { render json: { count: @improvement_action.get_downvotes.size } }
format.html {redirect_to :back}
end
end
Ansicht:
<%= link_to like_improvement_action_path(improvement_action), class:"btn btn-default stat-item like", method: :put, remote: true, data: { type: :json } do %>
<span class="fa fa-thumbs-up icon" aria-hidden="true"></span>
<span class="like_number"> <%= improvement_action.get_upvotes.size %></span>
<% end %>
<%= link_to unlike_improvement_action_path(improvement_action), class:"btn btn-default stat-item downvote", method: :put, remote: true, data: { type: :json } do %>
<span class="fa fa-thumbs-down icon" aria-hidden="true"></span>
<span class="unlike_number"> <%= improvement_action.get_downvotes.size %></span>
<% end %>
und die application.js:
$(document).ready(function() {
$('.like')
.on('ajax:success', function (e, data, status, xhr) {
$('.like_number').html(data.count)
})
.on('ajax:send', function() {
$(this).addClass('loading');
})
.on('ajax:complete', function() {
$(this).removeClass('loading');
})
.on('ajax:error', function (e, xhr, status, error) {
console.log(status);
console.log(error);
})
$('.downvote')
.on('ajax:success', function (e, data, status, xhr) {
$('.unlike_number').html(data.count);
})
.on('ajax:send', function() {
$(this).addClass('loading');
})
.on('ajax:complete', function() {
$(this).removeClass('loading');
})
.on('ajax:error', function (e, xhr, status, error) {
console.log(status);
console.log(error);
})
});
Ist es einer Person erlaubt, gleichzeitig zu wählen und dann zu downvoten, oder deaktivieren Sie den Downvote-Button, sobald der Nutzer auf Upvote klickt? – sahil
ja die Person kann upvote und dann downvote – terrorista
Was passiert mit der upvote Anzahl, wenn eine Person upvotes und dann downvote. Wird die Anzahl der Upvotes um 1 verringert und wird die Anzahl der Downvotes um 1 erhöht. – sahil