簡體   English   中英

檢查是否以特定投票權重投票給 Scope,並使用 Acts As Voteable

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

我在我的 Rails 應用程序中使用這些行為作為可投票的寶石,讓用戶可以選擇在不同范圍內對資源進行 1-5 評級。

例如,用戶可以對位置 1-5、價格 1-5 等進行評分。

我目前有這個代碼來檢查用戶是否評價了特定的 scope。 但是我怎樣才能檢查他們的投票有什么 vote_weight 。 即:他們的評分是 1、2、3、4 還是 5?

這是我的代碼,用於檢查他們是否對特定的 scope 進行了投票:

    <% 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

路線.rb

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

您可以創建一個類似於方法voted_on 的方法嗎? 但返回重量而不是檢查是否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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM