检查是否使用 Acts As Voteable 投票给具有特定投票权重的范围

Check if voted for Scope with a specific vote weight with Acts As Voteable

我在我的 rails 应用程序中使用 acts as votable gem,让用户可以选择在不同范围内对资源留下 1-5 的评级。

例如,用户可以对位置 1-5、价格 1-5 等进行评分。

我目前有这段代码来检查用户是否对特定范围进行了评级。但是我怎样才能检查 vote_weight 他们的投票结果。即:他们的评分是 1、2、3、4 还是 5?

这是我的代码,用于检查他们是否对特定范围进行了投票:

    <% if (user_signed_in?) && (current_user.voted_for? @stadium, :vote_scope => ‘location) %>
    <p>Current user has rated the location for this Stadium</p>    
    <% else %>
    <p>Current user has NOT rated the location for this Stadium</p>    
    <% end %>

这是我保存投票的代码:

<%= link_to like_stadium_path(@stadium, :score => 1, :vote_scope => ‘location’), method: :put do %>
<button class="mt-4 w-full border-primary-500 border border-transparent rounded-md py-3 px-8 flex items-center justify-center text-base font-medium text-primary-500 hover:bg-primary-700 hover:text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500">
Vote 1
</button>
<% end %>
<%= link_to like_stadium_path(@stadium, :score => 2, :vote_scope => 'location'), method: :put do %>
<button class="mt-4 w-full border-primary-500 border border-transparent rounded-md py-3 px-8 flex items-center justify-center text-base font-medium text-primary-500 hover:bg-primary-700 hover:text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500">
Vote 2
</button>
<% end %>
    
<%= link_to like_stadium_path(@stadium, :score => 3, :vote_scope => 'location'), method: :put do %>
<button class="mt-4 w-full border-primary-500 border border-transparent rounded-md py-3 px-8 flex items-center justify-center text-base font-medium text-primary-500 hover:bg-primary-700 hover:text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500">
Vote 3
</button>
<% end %>
    
<%= link_to like_stadium_path(@stadium, :score => 4, :vote_scope => 'location'), method: :put do %>
<button class="mt-4 w-full border-primary-500 border border-transparent rounded-md py-3 px-8 flex items-center justify-center text-base font-medium text-primary-500 hover:bg-primary-700 hover:text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500">
Vote 4
</button>
<% end %>
    
<%= link_to like_stadium_path(@stadium, :score => 5, :vote_scope => 'location'), method: :put do %>
<button class="mt-4 w-full border-primary-500 border border-transparent rounded-md py-3 px-8 flex items-center justify-center text-base font-medium text-primary-500 hover:bg-primary-700 hover:text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500">
Vote 5
</button>
<% end %>

stadiums_controller.rb

def upvote 
@stadium.upvote_by current_user, :vote_scope => params[:vote_scope], :vote_weight => params[:score]
redirect_back(fallback_location: root_path)
end 

def downvote #deletes the vote from DB
@stadium.unliked_by current_user, :vote_scope => params[:vote_scope]
redirect_back(fallback_location: root_path)
end

routes.rb

  resources :stadiums do
    member do
      put "like", to: "stadiums#upvote"
      put "unvote", to: "stadiums#downvote"
    end
  end

您可以创建一个类似于方法 voted_on? 的方法,但 return 权重而不是检查 exists?

class User < ApplicationRecord
  acts_as_voter

  def vote_weight_on(votable, vote_scope:)
    find_votes(
      votable_id: votable.id, 
      votable_type: votable.class.base_class.name, 
      vote_scope: vote_scope
    ).pluck(:vote_weight).first
  end
end


current_user.vote_weight_on(@stadium, vote_scope: "location")