2012-01-31 104 views
1

我找到this關於如何構建一個簡單的投票系統的教程。單擊投票上/下(Rails)後,簡單的投票系統不會添加投票數據庫?

我已經有一個用戶模型(包括只相關列)和我使用設計

create_table "posts", :force => true do |t| 
    t.string "content" 
    t.integer "user_id" 
    t.string "title" 
    t.integer "total_value", :default => 0 // Added by following the tutorial 
    end 

    create_table "users", :force => true do |t| 
    t.string "username" 
    end 

post.rb:

class Post < ActiveRecord::Base 
    attr_accessible :title, :content 

    belongs_to :user 
    has_many :comments, :dependent => :destroy 
    has_many :votes, :dependent => :destroy 
end 

user.rb:

class User < ActiveRecord::Base  
    has_many :posts, :dependent => :destroy 
    has_many :comments, :dependent => :destroy 
end 

vote.rb:

class Vote < ActiveRecord::Base 
    belongs_to :post 
end 

遷移:

class CreateVotes < ActiveRecord::Migration 
    def change 
    create_table :votes do |t| 
     t.integer :post_id 
     t.integer :user_id 
     t.boolean :value 

     t.timestamps 
    end 

    add_index :votes, [:post_id, :user_id] 
    end 
end 

votes_controller.rb

class VotesController < ApplicationController 
    def vote_up 
    check = Votes.find(:first, 
         :conditions => ["user_id = ? AND post_id = ?", session[:user_id], params[:id]]) 

    post = Post.find(params[:id]) 

    if check.nil? 
     vote = Votes.new 
     vote.post_id = params[:id] 
     vote.user_id = session[:user_id] 
     vote.value = true 
     vote.save 
     post.total_value += 1 
     post.save 
     render :text => post.total_value 
    elsif check.value == false 
     check.value = true 
     check.save 
     post.total_value += 2 
     post.save 
     render :text => post.total_value 
    else 
     render :text => "You have already voted up for this post." 
    end 
    end 

    def vote_down 
    check = Vote.find(:first, 
         :conditions => ["user_id = ? AND post_id = ?", session[:user_id], params[:id]]) 

    post = Post.find(params[:id]) 

    if check.nil? 
     vote = Vote.new 
     vote.post_id = params[:id] 
     vote.user_id = session[:user_id] 
     vote.value = true 
     vote.save 
     post.total_value -= 1 
     post.save 
     render :text => post.total_value 
    elsif check.value == true 
     check.value = false 
     check.save 
     post.total_value -= 2 
     post.save 
     render :text => post.total_value 
    else 
     render :text => "You have already voted down for this post." 
    end 
    end 
end 

的意見/頁/ index.html.erb:

<% for i in @posts %> 
    <h2><%= i.title %></h2> 
    <p><%= i.content %></p> 

    <div id="total_value_<%= i.id %>"><%= i.total_value %></div> 

    <%= link_to "Vote up", :url => {:controller => :votes, :action => :vote_up, :id => i.id}, 
          :update => "total_value_#{i.id}", 
          :remote => true %> 
    <%= link_to "Vote down", :url => {:controller => :votes, :action => :vote_down, :id => i.id}, 
          :update => "total_value_#{i.id}", 
          :remote => true %> 
<% end %> 

一切都顯示,沒有錯誤,但是當我點擊投票向上或否決,絕對沒有任何反應。

任何建議來解決這個問題?

編輯:

我沒有看到任何錯誤,也沒有保存/終端創建消息只是這樣的東西:

入門GET「/assets/application.js?body= 1「for 127.0.0.1 at 2012-02-01 06:47:50 +0800 Served asset /application.js - 304 Not Modified(0ms)[2012-02-01 06:47:50] WARN無法確定 響應主體的內容長度。響應或 設定的響應#集內容長度分塊=

+1

檢查Webrick服務器登錄控制檯。投票是否保存?你在sql查詢中看到它嗎? – Johny 2012-01-31 22:43:24

+0

@Johny沒有任何反應。終端只顯示如下內容:'開始GET「/assets/application.js?body=1」爲127.0.0.1在2012-02-01 06:47:50 + 0800' – alexchenco 2012-01-31 22:50:35

+0

@alexchenco你是如何解決問題的? – 2013-02-14 13:14:56

回答

1

這可能是一個錯字,但你不應該有,Vote.new,Vote.find,

相反,你必須真正的投票。 find and Votes.new

class VotesController < ApplicationController 
    def vote_up 
    check = Votes.find(:first, 
         :conditions => ["user_id = ? AND post_id = ?", session[:user_id], params[:id]]) 

    post = Post.find(params[:id]) 

    if check.nil? 
     vote = Votes.new 
     vote.post_id = params[:id] 
     vote.user_id = session[:user_id] 
     vote.value = true 
     vote.save 
     post.total_value += 1 
     post.save 
     render :text => post.total_value 
    elsif check.value == false 
     check.value = true 
     check.save 
     post.total_value += 2 
     post.save 
     render :text => post.total_value 
    else 
     render :text => "You have already voted up for this post." 
    end 
    end 

    def vote_down 
    check = Vote.find(:first, 
         :conditions => ["user_id = ? AND post_id = ?", session[:user_id], params[:id]]) 

    post = Post.find(params[:id]) 

    if check.nil? 
     vote = Vote.new 
     vote.post_id = params[:id] 
     vote.user_id = session[:user_id] 
     vote.value = true 
     vote.save 
     post.total_value -= 1 
     post.save 
     render :text => post.total_value 
    elsif check.value == true 
     check.value = false 
     check.save 
     post.total_value -= 2 
     post.save 
     render :text => post.total_value 
    else 
     render :text => "You have already voted down for this post." 
    end 
    end 
end 

將其改爲Vote.new和Vote.find並參閱。

UPDATE:

我建議你使用調試器的寶石。 另一個建議是使用這個功能的寶石。這裏有很多寶石。 vote_fu和acts_as_votable是兩個最常用的。

+0

嘿,你是對的。謝謝!但仍然得到相同的結果。 'Vote.all'仍然有0個實例(輸出'=> []')。 – alexchenco 2012-01-31 23:46:05

+0

我建議你使用'debugger'來查看問題。 – 2012-02-01 05:35:57