开发者

How do I prevent a form from being resubmitted too quickly in a rails application?

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.updat开发者_如何学Goe_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


In your comment class you could do:

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

This might not work, because I didn't test it, but it should send you in the right direction. What you're doing is:

Before a comment is created, load the last comment by that user. If it exists and was posted within the last 10 minutes add an error to the base with an explanation of why it couldn't be saved.

I hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜