单击投票/关闭(Rails)后,简单的投票系统不会对数据库添加投票?

时间:2012-01-31 22:39:34

标签: ruby-on-rails

我找到了关于如何构建简单投票系统的this教程。

我已经有用户发布模型(仅包含相关列),我正在使用设计

  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 %>

一切都显示,没有错误,但当我点击投票或投票时,绝对没有任何反应。

有任何解决此问题的建议吗?

修改

我没有看到任何错误,也没有在终端中保存/创建消息,只是这样:

  

在127.0.0.1开始GET“/assets/application.js?body=1”   2012-02-01 06:47:50 +0800服务资产/application.js - 304不   修改(0ms)[2012-02-01 06:47:50] WARN无法确定   响应体的内容长度。设置响应的内容长度或   设置Response#chunked = true

1 个答案:

答案 0 :(得分:1)

这可能是一个错字,但不应该有,Vote.new,Vote.find,

相反,你有Votes.find和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并查看。

<强>更新

我建议你使用调试器gem。 另一个建议是使用gem来实现此功能。为此创造了大量的宝石。 vote_fu和acts_as_votable是最常用的两个。