如何防止在Rails应用程序被重新提交太快一种形式?(How do I prevent a form

2019-10-21 09:44发布

我做了一个简单的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
  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

Answer 1:

尝试这个:

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


Answer 2:

在您的评论类,你可以这样做:

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分钟内添加错误与它为什么不能保存解释的基础。

我希望这有帮助!



文章来源: How do I prevent a form from being resubmitted too quickly in a rails application?