簡體   English   中英

Ruby on Rails中的Ajax / Increment Counter投票方法

[英]Ajax/Increment Counter voting methods in Ruby on Rails

我不確定自己在做什么錯,非常困惑。 我正在嘗試使評分計數器像舊的Digg.com評分計數器一樣。 誰能看看我的代碼並為我提供幫助?

需要注意的是,這是單獨的數據庫表/模型: 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 %>")

查看thumbs_up gem。 有了這個gem,您只需要將其包含在模型中:

  • act_as_voteable

並向控制器添加一些邏輯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM