Ruby on rails 如何防止表单在rails应用程序中重新提交过快?
我制作了一个简单的Rails应用程序,允许人们对帖子发表评论。如何防止该用户反复提交该表单?在reddit.com上,他们只允许新用户每十分钟发布一次新帖子。我如何用我的简单博客/评论系统做到这一点?任何帮助都将不胜感激。谢谢你阅读我的问题。 编辑:我试图在没有用户模型的情况下完成这项工作 以下是我当前的评论:Ruby on rails 如何防止表单在rails应用程序中重新提交过快?,ruby-on-rails,ruby,forms,Ruby On Rails,Ruby,Forms,我制作了一个简单的Rails应用程序,允许人们对帖子发表评论。如何防止该用户反复提交该表单?在reddit.com上,他们只允许新用户每十分钟发布一次新帖子。我如何用我的简单博客/评论系统做到这一点?任何帮助都将不胜感激。谢谢你阅读我的问题。 编辑:我试图在没有用户模型的情况下完成这项工作 以下是我当前的评论: class CommentsController < ApplicationController # before_filter :require_user, :only =&
class CommentsController < ApplicationController
# before_filter :require_user, :only => [:index, :show, :new, :edit]
before_filter :post_check
def record_post_time
cookies[:last_post_at] = Time.now.to_i
end
def last_post_time
Time.at((cookies[:last_post_at].to_i rescue 0))
end
MIN_POST_TIME = 2.minutes
def post_check
return true if (Time.now - last_post_time) > MIN_POST_TIME
# handle error
# flash("Too many posts")
end
def index
@message = Message.find(params[:message_id])
@comments = @message.comments
end
def show
@message = Message.find(params[:message_id])
@comment = @message.comments.find(params[:id])
end
def new
@message = Message.find(params[:message_id])
@comment = @message.comments.build
end
def edit
@message = Message.find(params[:message_id])
@comment = @message.comments.find(params[:id])
end
def create
@message = Message.find(params[:message_id])
@comment = @message.comments.build(params[:comment])
#@comment = Comment.new(params[:comment])
if @comment.save
record_post_time#
flash[:notice] = "Replied to \"#{@message.title}\""
redirect_to(@message)
# redirect_to post_comment_url(@post, @comment) # old
else
render :action => "new"
end
end
def update
@message = Message.find(params[:message_id])
@comment = Comment.find(params[:id])
if @comment.update_attributes(params[:comment])
record_post_time
redirect_to post_comment_url(@message, @comment)
else
render :action => "edit"
end
end
def destroy
end
end
class CommentsController[:index,:show,:new,:edit]
前\u过滤器:后\u检查
def记录后时间
cookies[:last\u post\u at]=Time.now.to\u i
结束
def上次发布时间
Time.at((cookies[:last_post_at].to_i rescue 0))
结束
最小后时间=2.5分钟
def后检查
如果(Time.now-last_post_Time)>MIN_post_Time,则返回true
#处理错误
#flash(“帖子太多”)
结束
def索引
@message=message.find(参数[:message\u id])
@comments=@message.comments
结束
def秀
@message=message.find(参数[:message\u id])
@comment=@message.comments.find(参数[:id])
结束
def新
@message=message.find(参数[:message\u id])
@comment=@message.comments.build
结束
定义编辑
@message=message.find(参数[:message\u id])
@comment=@message.comments.find(参数[:id])
结束
def创建
@message=message.find(参数[:message\u id])
@comment=@message.comments.build(参数[:comment])
#@comment=comment.new(参数[:comment])
if@comment.save
记录发布时间#
flash[:notice]=“回复\”{@message.title}”
将_重定向到(@message)
#重定向到post_comment_url(@post,@comment)#old
其他的
呈现:操作=>“新建”
结束
结束
def更新
@message=message.find(参数[:message\u id])
@comment=comment.find(参数[:id])
if@comment.update_属性(参数[:comment])
记录发布时间
重定向到post\u comment\u url(@message,@comment)
其他的
呈现:操作=>“编辑”
结束
结束
def销毁
结束
结束
在评论类中,您可以执行以下操作:
validate :posting_too_often
def posting_too_often
c = self.post.comments.find_by_user_id(self.user_id, :limit => 1, :order => 'created_at desc')
if c && c.created_at > 10.minutes.ago
self.errors.add_to_base("stop posting so many crappy comments!")
end
end
这可能不起作用,因为我没有对它进行测试,但它应该会让您朝着正确的方向前进。您正在做的是:
在创建注释之前,加载该用户的最后一条注释。如果它存在并且在过去10分钟内发布,请向基础添加一个错误,并解释为什么无法保存该错误
我希望这有帮助 试试这个:
class CommentsController < ApplicationController
before_filter :post_check
def record_post_time
cookies[:last_post_at] = Time.now.to_i
end
def last_post_time
Time.at((cookies[:last_post_at].to_i rescue 0))
end
MIN_POST_TIME = 2.minutes
def post_check
return true if (Time.now - last_post_time) > MIN_POST_TIME
flash[:notice] = "Too many comments makes you a busy cat!"
@message = Message.find(params[:message_id])
redirect_to(@message)
return false
end
def create
@comment = Comment.new(params[:comment])
if @comment.save
record_post_time
else
end
end
def update
@comment = Comment.find(parms[:id])
if @comment.update_attributes(params[:comment]))
record_post_time
else
end
end
end
class CommentsControllerMIN_post_Time,则返回true
flash[:notice]=“评论太多会让你忙得不可开交!”
@message=message.find(参数[:message\u id])
将_重定向到(@message)
返回错误
结束
def创建
@comment=comment.new(参数[:comment])
if@comment.save
记录发布时间
其他的
结束
结束
def更新
@comment=comment.find(参数[:id])
if@comment.update_属性(参数[:comment]))
记录发布时间
其他的
结束
结束
结束
谢谢您的建议。不过,我正试图在没有用户的情况下做到这一点。人们可以匿名发表评论。这可以通过cookie实现吗?通过他们的IP地址实现,所以将其更改为:self.post.comments.find_by_IP(…)。嗨,我已经尝试过了,但似乎无法实现。显然,create和update方法都在控制器中,但是其余的代码都在注释模型中吗?对不起,我是ruby和rails的新手。谢谢你的帮助。代码转到CommentsController。谢谢你的帮助。我在尝试创建注释时遇到了一个错误:“没有从nil隐式转换为float”。与“return true if(Time.now-session[:last\u post\u at])>MIN\u post\u Time”行和减号运算符有关。好的,我将“return true if(Time.now-session[:last\u post\u at])>MIN\u post\u Time”更改为:“return true if(Time.now.to\u-session[:last\u post\u at].to\u.to\f.”to\u-session现在我可以在我的评论控制器中使用您的代码进行评论,但我仍然可以每两分钟进行一次更频繁的评论。仍然可以让我尽快发布评论…:)谢谢你的帮助。