【问题标题】:acts_as_votable not working when I upvote/downvote当我赞成/反对时,acts_as_votable 不起作用
【发布时间】:2015-12-09 15:38:02
【问题描述】:

我试图让treatment 可以通过赞成票或反对票投票,但 act_as_votable 提供的方法似乎没有按预期工作。当我尝试显示 upvotes-downvotes 的总和时,它每次都返回 0,这意味着投票没有像应有的那样注册。

我的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 响应是什么?之后数据库中有投票吗?
  • @Kristján 我将服务器日志添加到原始帖子中
  • 您的日志显示没有INSERTs 进行新投票。是@current_user.nil? 应该是current_user 吗?
  • 我觉得你是对的,我把它改成了current_user。但我现在收到错误 undefined local variable or method current_user' `,即使它是在 ApplicationHelper 中以另一种方法 def signed_in? 定义的
  • Helper 不会加载到控制器中,除非您明确地 included 并命名相同的 doesn't make them the same。您可能想在ApplicationController 中编写current_user 方法,或者运行设置@current_user 的before_action,这将在操作期间持续存在。

标签: ruby-on-rails ruby ruby-on-rails-4 voting acts-as-votable


【解决方案1】:

感谢 Kristjan 的帮助,我意识到没有发生插入。我在 upvote 和 downvote 方法中定义了 current_user。修改后的工作版本是这样的。

 def upvote
  current_user = User.find_by_id(session[:user_id])
  @treatment = Treatment.find(params[:id])
  @treatment.upvote_by current_user

  render 'show'
end

def downvote
  current_user = User.find_by_id(session[:user_id])
  @treatment = Treatment.find(params[:id])
  @treatment.downvote_by current_user

  render 'show'
end

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 2015-10-21
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多