轨道:评级向上或向下计数器第二部分

发布于 2025-01-08 10:21:42 字数 1398 浏览 0 评论 0原文

基本要点: 我目前正在尝试创建一个具有向上或向下功能的评级计数器。我创建了一个单独的模型来显示评级计数器,并且该计数器属于该帖子。我正在尝试创建一些类似于 Reddit 甚至 Stackoverflow 的东西。我目前陷入了该做什么的困境。谢谢大家。

DB:评级表:post_id、user_id、评级

DB:帖子表:评级_count

评级模型

class Rating < ActiveRecord::Base
  attr_accessible :post_id, :user_id, :ratings
  has_many :post
  has_many :users

  validates :post_id, presence: true
  validates :user_id, presence: true
end

帖子模型

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

  validates :user_id, presence: true
  validates :smiles, presence: true
end

评级控制器

Nothing in it

帖子控制器

class PostsController < ApplicationController

  def rate
    @post = post.find(params[:id])
    if params[:ratings_count]
      @[email protected]_count+1
    end
  end

  def unrate

    unsure

  end
end

评级表

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

Basic Gist:
I am currently trying to create a rating counter that has a up or down feature to it. I create d a separate model to show the rating counter and the counter belongs to the post. I am trying to create something along the lines of Reddit or even Stackoverflow's. I am currently stuck on what to do. Thank you everyone.

DB:Rating Table: post_id, user_id, ratings

DB:Post Table: ratings_count

Rating Model

class Rating < ActiveRecord::Base
  attr_accessible :post_id, :user_id, :ratings
  has_many :post
  has_many :users

  validates :post_id, presence: true
  validates :user_id, presence: true
end

Post Model

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

  validates :user_id, presence: true
  validates :smiles, presence: true
end

Rating Controller

Nothing in it

Post Controller

class PostsController < ApplicationController

  def rate
    @post = post.find(params[:id])
    if params[:ratings_count]
      @[email protected]_count+1
    end
  end

  def unrate

    unsure

  end
end

Rating Form

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

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

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

发布评论

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

评论(1

幽梦紫曦~ 2025-01-15 10:21:42

你可以使用这样的东西:
http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000348

class PostsController < ApplicationController

  def rate
    Post.increment_counter(:ratings_count, params[:id]) if params[:ratings_count]
  end

  def unrate
    # You will obviously need to check for a ratings_down or something similar field in your form
    Post.decrement_counter(:ratings_count, params[:id]) if params[:ratings_count]
  end
end

You could use something like this:
http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000348

class PostsController < ApplicationController

  def rate
    Post.increment_counter(:ratings_count, params[:id]) if params[:ratings_count]
  end

  def unrate
    # You will obviously need to check for a ratings_down or something similar field in your form
    Post.decrement_counter(:ratings_count, params[:id]) if params[:ratings_count]
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文