Ruby on Rails 中的 Ajax/Increment Counter 投票方法
我不确定自己做错了什么,非常困惑。我正在尝试制作一个像旧的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 thumbs_up gem。
有了这个 gem,您只需将其包含到您的模型中:
并向您的控制器添加一些逻辑
check out the thumbs_up gem.
with this gem you just need to include this to your model:
and add some logic to your controller