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

发布于 2025-01-08 02:32:06 字数 1578 浏览 0 评论 0原文

我不确定自己做错了什么,非常困惑。我正在尝试制作一个像旧的 Digg.com 评级计数器一样的评级计数器。任何人都可以看一下我的代码并帮助我吗?

需要注意的是,这里是单独的数据库表/模型:Rate、Post、User

Rate: user_id 整数、post_id 整数和 ratings_count 整数 default => 0

帖子: ratings_count 整数默认 => 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 %>")

I am unsure of what I am doing wrong, very confused. I am trying to make a rating counter like the old Digg.com rating counter. Can anyone take a look at my code and help me out?

Things to note is that here are the separate database tables/models: Rate, Post, User

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

Post: ratings_count integer default => 0

This is what I am currently working with, all the code that I have for this rating system, if it is not shown then I don't have it and thus missing and would love if someone can help point out what is missing. Thank you.

Rate Controller

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

Rate Model

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

Rate Form

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

Micropost Model

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

  validates :ratings, presence: true
end

create.js.erb for rate

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2025-01-15 02:32:06

查看 thumbs_up gem。
有了这个 gem,您只需将其包含到您的模型中:

  • acts_as_voteable

并向您的控制器添加一些逻辑

check out the thumbs_up gem.
with this gem you just need to include this to your model:

  • acts_as_voteable

and add some logic to your controller

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文