2012-02-22 48 views
0

我不確定我做錯了什麼,很困惑。我正在嘗試製作一個評分計數器,如舊的Digg.com評分計數器。任何人都可以看看我的代碼並幫助我嗎?Ruby on Rails中的Ajax/Increment Counter投票方法

這裏需要注意的是,這裏有單獨的數據庫表/型號:Rate, Post, User

率:user_id integer, post_id integer and ratings_count integer default => 0

帖子:ratings_count integer default => 0

這就是我目前的工作,所有的代碼,我有這個評級系統,如果它沒有顯示,那麼我沒有它,因此失蹤,並會愛如果有人能幫助指出什麼是缺少的。謝謝。

速率控制器

class RateController < ApplicationController 
    def create 
    @post.increment_counter(:ratings_count, params[:id]) if params[:ratings_count] 
    respond_to do |format| 
     format.html { redirect_to @user } 
     format.js 
    end 
    end 

    def increment_counter(counter_name, id) 
    update_counters(:post, :ratings_count => 1) 
    end 
end 

速率模型

class Rate < ActiveRecord::Base 
    attr_accessible :post_id, :user_id, :ratings_count 
    has_many :posts 
    has_many :users 

    validates :post_id, presence: true 
    validates :user_id, presence: true 

end 

率形式

<%=form_for @rate do |f|%> 
<%= f.hidden_field :ratings_count %> 
<%=f.submit "Rate"%> 
<%end%> 

型號微柱

class Micropost < ActiveRecord::Base 
    attr_accessible :ratings_count 
    belongs_to :user 
    has_many :ratings 

    validates :ratings, presence: true 
end 

create.js.erb的速度

$("#rates").html("Rates: <%= @micropost.ratings_count.count %>") 

回答

2

退房的thumbs_up寶石。 這個寶石,你只需要包括這模型:

  • acts_as_voteable

,並添加一些邏輯控制器