Ruby on rails 导轨:额定值向上或向下计数器第二部分
基本要点: 我目前正在尝试创建一个具有向上或向下功能的评级计数器。我创建了一个单独的模型来显示评级计数器,计数器属于帖子。我正试图按照Reddit甚至Stackoverflow的思路创建一些东西。我现在正忙着做什么。谢谢大家 DB:评级表:post_id、用户_id、评级 DB:Post表格:额定值\U计数 评级模型Ruby on rails 导轨:额定值向上或向下计数器第二部分,ruby-on-rails,ruby,ruby-on-rails-3,counter,rating,Ruby On Rails,Ruby,Ruby On Rails 3,Counter,Rating,基本要点: 我目前正在尝试创建一个具有向上或向下功能的评级计数器。我创建了一个单独的模型来显示评级计数器,计数器属于帖子。我正试图按照Reddit甚至Stackoverflow的思路创建一些东西。我现在正忙着做什么。谢谢大家 DB:评级表:post_id、用户_id、评级 DB:Post表格:额定值\U计数 评级模型 class Rating < ActiveRecord::Base attr_accessible :post_id, :user_id, :ratings has_
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]
@post.ratings_count=@post.ratings_count+1
end
end
def unrate
unsure
end
end
class PostsController
评级表
<%=form_for @post, :action=>"rate" do |f|%>
<%= f.hidden_field :ratings_count %>
<%=f.submit "Rate"%>
<%end%>
“速率”do | f |%>
您可以使用以下内容:
class PostsController
潜在相关阅读:我不确定是否正确设置了表单?你能核实一下吗?谢谢,不,只是没有增加。您是否通过ajax提交此表单?如果是这样,请在Firefox Firebug或ChromeNo ajax中的默认控制台中查看您的控制台,但是,嗯,在我的分级模型中,分级整数、post_id和user_id没有填写,是否缺少一些内容?由于def比率仅增加post模型中的评级计数,因此在您的评级模型中将has\u many:post
更改为has\u many:post
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