#65 Stopping Spam with Akismet
Aug 06, 2007 | 11 minutes |
The Railscasts site has been getting a lot of comment spam in the past, but no longer. In this episode I will show you how I solved this problem by using the Akismet web service.
Resources
- Akismet API Documentation
- Wordpress API Key
- Akismetor Plugin
- Episode 52: Update through Checkboxes
- Episode 35: Custom REST Actions
comments_controller.rb
def create
@comment = Comment.new(params[:comment])
@comment.request = request
if @comment.save
if @comment.approved?
flash[:notice] = "Thanks for the comment."
else
flash[:error] = "Unfortunately this comment is considered spam by Akismet. " +
"It will show up once it has been approved by the administrator."
end
redirect_to episode_path(@comment.episode_id)
else
render :action => 'new'
end
end
def destroy_multiple
Comment.destroy(params[:comment_ids])
flash[:notice] = "Successfully destroyed comments."
redirect_to comments_path
end
def approve
@comment = Comment.find(params[:id])
@comment.mark_as_ham!
redirect_to comments_path
end
def reject
@comment = Comment.find(params[:id])
@comment.mark_as_spam!
redirect_to comments_path
end
def create @comment = Comment.new(params[:comment]) @comment.request = request if @comment.save if @comment.approved? flash[:notice] = "Thanks for the comment." else flash[:error] = "Unfortunately this comment is considered spam by Akismet. " + "It will show up once it has been approved by the administrator." end redirect_to episode_path(@comment.episode_id) else render :action => 'new' end end def destroy_multiple Comment.destroy(params[:comment_ids]) flash[:notice] = "Successfully destroyed comments." redirect_to comments_path end def approve @comment = Comment.find(params[:id]) @comment.mark_as_ham! redirect_to comments_path end def reject @comment = Comment.find(params[:id]) @comment.mark_as_spam! redirect_to comments_path end
models/comment.rb
before_create :check_for_spam
def request=(request)
self.user_ip = request.remote_ip
self.user_agent = request.env['HTTP_USER_AGENT']
self.referrer = request.env['HTTP_REFERER']
end
def check_for_spam
self.approved = !Akismetor.spam?(akismet_attributes)
true # return true so it doesn't stop save
end
def akismet_attributes
{
:key => 'abc123',
:blog => 'http://railscasts.com',
:user_ip => user_ip,
:user_agent => user_agent,
:comment_author => name,
:comment_author_email => email,
:comment_author_url => site_url,
:comment_content => content
}
end
def mark_as_spam!
update_attribute(:approved, false)
Akismetor.submit_spam(akismet_attributes)
end
def mark_as_ham!
update_attribute(:approved, true)
Akismetor.submit_ham(akismet_attributes)
end
before_create :check_for_spam def request=(request) self.user_ip = request.remote_ip self.user_agent = request.env['HTTP_USER_AGENT'] self.referrer = request.env['HTTP_REFERER'] end def check_for_spam self.approved = !Akismetor.spam?(akismet_attributes) true # return true so it doesn't stop save end def akismet_attributes { :key => 'abc123', :blog => 'http://railscasts.com', :user_ip => user_ip, :user_agent => user_agent, :comment_author => name, :comment_author_email => email, :comment_author_url => site_url, :comment_content => content } end def mark_as_spam! update_attribute(:approved, false) Akismetor.submit_spam(akismet_attributes) end def mark_as_ham! update_attribute(:approved, true) Akismetor.submit_ham(akismet_attributes) end

