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