在滑轨中添加按钮单击计数器3 [英] Adding button click counter in rails 3
本文介绍了在滑轨中添加按钮单击计数器3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在文章上添加按钮,以便知道它在数据库中被点击和更新计数器的次数,我正在使用mongoid,我的模型是:
I want to add buttons on my article so that I can know the number of times its clicked and update counter on the database, I am using mongoid ,my model is:
class Article
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type => String
field :content, :type => String
field :likes, :type => Integer ,:default => 0
field :dislikes, :type =>Integer, :default => 0
field :spam, :type => Integer, :default => 0
end
我的文章显示控制器为:
My articles show controller is:
def show
@article = Article.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @article }
end
end
我的显示视图是:
<p id="notice"><%= notice %></p>
<p>
<b>Title:</b>
<%= @article.title %>
</p>
<p>
<b>Content:</b>
<%= raw @article.content %>
</p>
Likes : <%= @article.likes %> <br/>
Dislikes : <%= @article.dislikes %><br/>
Spams : <%= @article.spam %><br/>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
我在互联网上找到任何有关它的东西.
I find anything about it in internet.
我该如何实现?
推荐答案
最简单的方法是在Article
模型中添加click_count
整数属性,然后在控制器代码中增加该值:
The easiest thing to do would be add a click_count
integer attribute to your Article
model and then increment this in your controller code:
def show
@article = Article.find(params[:id])
@article.increment! :click_count
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @article }
end
end
这篇关于在滑轨中添加按钮单击计数器3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文