0
我想允許treatment
與upvotes或downvotes投票,但由act_as_votable提供的方法似乎沒有按預期工作。當我嘗試顯示upvotes-downvotes的總和時,它每次返回0,暗示投票不會像他們應該註冊的那樣。acts_as_votable不工作時,我upvote/downvote
我treatments_controller.rb
def upvote
@treatment = Treatment.find(params[:id])
@treatment.upvote_by @current_user
render 'show'
end
def downvote
@treatment = Treatment.find(params[:id])
@treatment.downvote_by @current_user
redirect_to treatments_path
end
我treatment.rb
class Treatment < ActiveRecord::Base
acts_as_votable
has_many :review
has_one :service
def score
self.get_upvotes.size - self.get_downvotes.size
end
end
我routes.rb
resources :treatments do
member do
put "like", to: "treatments#upvote"
put "dislike", to: "treatments#downvote"
end
end
而且我treatments/show.html.erb
<div class="container">
<div class="row">
<div class="col-sx-12 col-sm-12 col-md-8">
<h3><%= @treatment.name %></h3></br></br>
<h3>
<%= link_to "upvote", like_treatment_path(@treatment), method: :put %></br>
<%= link_to "downvote", dislike_treatment_path(@treatment), method: :put %></br>
<%= @treatment.score %></br> </h3>
服務器日誌說以下
Started PUT "/treatments/1/like" for ::1 at 2015-12-09 16:22:14 +0000
Processing by TreatmentsController#upvote as HTML
Parameters: {"authenticity_token"=>"hQnqV+OZ6m/9kFnr5YH6j563DIjyKfA4K/8fH6kpFbXbJRbsHCcNYn1AX4usZmUNXPlnFpxHMwTM8ilFxcbdhQ==", "id"=>"1"}
Geokit is using the domain: localhost
Treatment Load (0.5ms) SELECT "treatments".* FROM "treatments" WHERE "treatments"."id" = ? LIMIT 1 [["id", 1]]
(0.5ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 1], ["votable_type", "Treatment"], ["vote_flag", "t"]]
(0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 1], ["votable_type", "Treatment"], ["vote_flag", "f"]]
Rendered treatments/show.html.erb within layouts/application (6.5ms)
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 4]]
Completed 200 OK in 569ms (Views: 563.3ms | ActiveRecord: 1.0ms)
當你點擊up/downvote時看看你的服務器日誌 - 看到有什麼不對嗎?什麼是HTTP響應?之後在DB中有沒有投票? –
@Kristján我將服務器日誌添加到原來的帖子 –
您的日誌顯示新的投票沒有INSERT。是'@ current_user.nil?'應該是'current_user'嗎? –