轨道:评级向上或向下计数器第二部分
基本要点: 我目前正在尝试创建一个具有向上或向下功能的评级计数器。我创建了一个单独的模型来显示评级计数器,并且该计数器属于该帖子。我正在尝试创建一些类似于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以使用这样的东西:
http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000348
You could use something like this:
http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000348