2010-11-15 69 views

回答

3

它們設置起來非常簡單。這裏有一個你可以開始:

class Object < ActiveRecord::Base 
    has_many :votes, :as => :votable 
    has_many :voting_users, 
       :through => :votes, 
       :source => :user 

#object_controller 

    def vote_up 
    get_vote 
    @vote.value += 1 unless @vote.value == 1 
    @vote.save 
    respond_to do |format| 
     format.html {render :action => 'view'} 
     format.js { render :action => 'vote'} 
    end 
    end 

    private 

    def get_vote 
    current_object = Objects.detect{|r| r.id == params[:id].to_i} 
    @object = current_object 
    @vote = current_object.votes.find_by_user_id(current_user.id) 
    unless @vote 
     @vote = Vote.create(:user_id => current_user.id, :value => 0) 
     current_object.votes << @vote 
    end 
    end 
+0

太棒了,感謝您的代碼。我們可能會把它放在這個方向。 – marketer 2010-11-17 01:29:58

+0

我是在同一個地方,找不到寶石,所以只寫了這個。 – Trip 2010-11-17 19:14:51

1

Make_votable看起來像一個真棒和易於實施的寶石。

1

我使用的是Thumbs Up,它的功能就像一個魅力。這是一個改良的vote_fu Rails 3.