如何防止表单在Rails应用程序中过快重新提交? [英] How do I prevent a form from being resubmitted too quickly in a rails application?

查看:118
本文介绍了如何防止表单在Rails应用程序中过快重新提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个简单的Rails应用程序,允许人们对帖子发表评论。如何防止该用户反复提交该表单?在reddit.com上,他们只允许新用户每十分钟发布一次新帖子。我如何使用我的简单博客/评论系统来做到这一点?任何帮助将不胜感激。感谢您阅读我的问题。
编辑:我试图在没有用户模型的情况下完成此操作。



这是我目前的评论控制器:

  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
结束

def last_post_time
Time.at((cookies [:last_post_at] .to_i rescue 0))
结束

MIN_POST_TIME = 2 .minutes

def post_check
返回true if(Time.now - last_post_time)> MIN_POST_TIME

#处理错误
#flash(帖子太多)
结束

def索引
@message = Message.find (params [:message_id])
@comments = @ message.comments
end
$ b def show
@message = Message.find(params [:message_id])
@comment = @ message.comments.find(params [:id])
end
$ b def new
@message = Message.find(params [:message_id ])
@comment = @ message.comments.build
end
$ b def编辑
@message = Message.find(params [:message_id])
@comment = @ message.comments.find(params [:id])
结束
$ b def创建
@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] =已回复\#{@ message.title} \
redirect_to(@message)
#re direct_to post_comment_url(@post,@comment)#old
else
render:action => 新
结束
结束

def更新
@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< ApplicationController 
before_filter:post_check
def record_post_time
cookies [:last_post_at] = Time.now.to_i
end
def last_post_time
Time.at((cookie [:last_post_at] .to_i rescue 0))
end
MIN_POST_TIME = 2.minutes
def post_check
返回true if(Time.now - last_post_time)> MIN_POST_TIME
flash [:notice] =太多评论让你成为一只繁忙的猫!
@message = Message.find(params [:message_id])
redirect_to(@message)
返回false
结束
def创建
@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


I have made a simple Rails application that allows people to comment on posts. How do I prevent that user from submitting that form over and over again? On reddit.com they only allow newer users to make new posts every ten minutes. How can I do this with my simple blog/commenting system? Any help would be greatly appreciated. Thanks for reading my question. EDIT: I'm trying to accomplish this without a user model.

Here's my current Comments controller:

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

解决方案

Try this:

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

这篇关于如何防止表单在Rails应用程序中过快重新提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆