简体   繁体   English

RoR投票系统。 按条件选票计数选票=真或选票=假不起作用

[英]RoR voting system. Counting votes by condition vote = true or vote = false not working

I have these models 我有这些模特

  • Issue 问题
  • Vote 投票

Issue has_many votes and Vote belongs_to issue. 发出has_many票,并投票属居。 The Vote model has a boolean vote attribute. 投票模型具有布尔投票属性。 On the issues index view I want to cycle through the issues and display the title, body, an up vote button, a down vote button, and respective labels that show how many up votes and down votes there currently is. 在问题索引视图中,我想循环浏览问题,并显示标题,正文,向上投票按钮,向下投票按钮,以及分别显示当前有多少向上和向下投票的标签。 I do this with a form with hidden fields for issue_id and vote (1 or 0). 我使用带有issue_id和表决(1或0)隐藏字段的表单来执行此操作。 Methods on the Issue model are supposed to count the votes. 发行模型的方法应该计算票数。 But I keep getting 0 returned. 但是我一直得到0返回。 Totalvotes_count works but the other two don't. Totalvotes_count有效,而其他两个无效。 In the server log I see the votes being created with the proper issue_id and vote value but the queries aren't working for some reason. 在服务器日志中,我看到使用正确的issue_id和vote值创建的表决,但是由于某些原因查询不起作用。

Issue model 问题模型

class Issue < ActiveRecord::Base
attr_accessible :body, :end_at, :title
validates_presence_of :title, :body, :end_at
has_many :votes

def upvotes_count
    votes.count(:conditions => "vote = 1")
end

def downvotes_count
    votes.count(:conditions => "vote = 0")
end

def totalvotes_count
    votes.count
end

end

index.html.erb index.html.erb

<% @issues.each do |issue| %>
<li>
    <div class="issue">
        <h2><%= issue.title %></h2>
        <p><%= issue.body %></p>

        <%= form_for(@vote, :remote => true) do |f| %>
        <%= f.hidden_field "issue_id", :value => issue.id %>
        <%= f.hidden_field "vote", :value => 1 %>
        <%= submit_tag issue.upvotes_count.to_s + " Up", :class => 'up-vote' %>
        <% end %>

        <%= form_for(@vote, :remote => true) do |f| %>
        <%= f.hidden_field "issue_id", :value => issue.id %>
        <%= f.hidden_field "vote", :value => 0 %>
       <%= submit_tag issue.downvotes_count.to_s + " Down", :class => 'down-vote' %>
        <% end %>

    </div>
</li>
<% end %>

Votes Controller 投票控制人

class VotesController < ApplicationController

  def index
    @votes = Vote.find(:all, :include => :issue)
  end

  def new 
    @vote = Vote.new(params[:vote])

    respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @vote }
    end
  end

  def create
    @vote = Vote.new(params[:vote])

    respond_to do |format|
      if @vote.save
          format.js
          format.html { redirect_to issues_path }
      else
          format.html { redirect_to issues_path }
      end
    end
  end

end

Issues controller (partial) 问题控制器(部分)

class IssuesController < ApplicationController
  # GET /issues
  # GET /issues.json
  def index
    @issues = Issue.all

    @vote = Vote.new


    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @issues }
    end

end 结束

I believe your problem is that you do not have " self " called on your methods in the model, but as tamersalama mentioned this is likely overkill for simple vote tracking. 我相信您的问题是您没有在模型中的方法上调用“ self ”,但是正如tamersalama所提到的那样,这对于简单的投票跟踪而言可能是过大的。 It is probably just easiest to write a simple +1 method on an :upvote and :downvote attribute. :upvote:downvote属性上编写简单的+1方法可能是最简单的。

What is the default value of vote . vote的默认值为多少? If it's NULL - then neither would work. 如果为NULL,则两者都不起作用。

Reading the question more carefully - it looks like the value in vote determine if it's an up or downvote. 更仔细地阅读问题-看起来vote的价值决定了它是vote还是反对。 I suggest you'd use a STI (single-table inheritance) mechanism for Vote , where you'd create a type column to store the type of vote (either :upvote, :downvote), with an index on the type attribute. 我建议您对Vote使用STI(单表继承)机制,在其中创建一个类型列以存储投票类型(:upvote,:downvote),并在type属性上建立索引。

All of this however seems like an overkill (depending on the rest of your domain). 但是,所有这一切似乎都不过分(取决于您域的其余部分)。 You could simply cache votes along with each of the issues. 您只需将投票连同每个问题一起进行缓存。 A column for upvote and one for downvote would suffice. 一列赞成票和一票赞成票就足够了。 Unless you'd want to track other attributes on Vote (like people who upvotes for example). 除非您想跟踪Vote上的其他属性(例如,喜欢投票的人)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RoR投票系统。 如何计算上,下,总票数? - RoR voting system. How to count up votes, down votes, and total votes? 简单的投票系统没有在单击上/下投票(Rails)后向数据库添加投票? - Simple voting system doesn't add vote to database after clicking vote up/down (Rails)? 没有 gem 的简单投票系统 - 如何跟踪赞成票或反对票并允许每个 user_id 投一票 - Simple voting system without a gem - how to track up or down vote and allow one vote per user_id 赞许投票系统 - Thumbs Up vote system model 投票系统问题 - model problems in vote system Rails 3-选民投票-用户投票的集合 - Rails 3 - vote_fu - Collection of User Votes Activerecord-信誉系统Rails gem如何按投票和投票的新近排序? - Activerecord-reputation-system Rails gem how to sort by votes and recency of vote? 如何开发一个用户可以投票给其他用户的投票系统(即类似声誉的系统)? - How can I develop a voting system where users can vote other users (i.e a reputation-like system)? RoR投票系统在本地工作,但在Heroku上部署时则不行 - RoR Voting system working locally but not when deployed on Heroku 我的第一个RoR Gem:MicropostsController中的NameError#up_vote uninitialized constant VoterLove :: Voter :: Vote - My first RoR Gem: NameError in MicropostsController#up_vote uninitialized constant VoterLove::Voter::Vote
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM