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