Rails宝典之第六十五式: Stopping spam

这次要介绍的是使用[url=http://akismet.com]Akismet[/url]来预防垃圾comment
akismetor是作者写的一个插件:[url]http://svn.railscasts.com/public/plugins/akismetor[/url]

使用akismetor需要如下几步

1,给comments表添加几个字段:
[code]
add_column :comments, :user_ip, :string
add_column :comments, :user_agent, :string
add_column :comments, :referrer, :string
add_column :comments, :approved, :boolean, :default => false, :nul => false
Comment.update_all("approved=1")
[/code]

2,routes.rb:
[code]
map.resources :comments, :collection => { :destroy_multiple => :delete },
:member => { :approve => :put, :reject => :put}
[/code]

3,comment.rb:
[code]
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
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

def self.recent(limit, conditions = nil)
find(:all, :limit => limit, :conditions => conditions, :order => 'created_at DESC')
end
[/code]

4,comments_controller.rb:
[code]
def index
@approved_comments = Comment.recent(20, :approved => true)
@rejected_comments = Comment.recent(100, :approved => false ) if admin?
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(parmas[: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
[/code]

5,comments/index.rhtml:
[code]
<% title "Recent Comments" %>

<div class="content comments">
<%= render :partial => 'comment', :collection => @approved_comments, :spacer_template => 'divider' %>
</div>

<% if admin? %>
<div class="content" id="rejected_comments">
<% form_tag destroy_multiple_comments_path, :method => :delete do %>
<h3>Rejected Comments</h3>
<table>
<% for comment in @rejected_comments %>
<tr>
<td><%= check_box_tag "comment_ids[]", comment.id, true %></td>
<td><%= link_to h(comment.name), comment.site_url %></td>
<td><%= h truncate(comment.content, 30) %></td>
<td><%= link_to "not spam", approve_comment_path(comment), :confirm => 'Are you sure?', :method => :put %></td>
</tr>
<% end %>
</table>
<p><%= submit_tag "Destroy Checked" %></p>
<% end %>
</div>
<% end %>
[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值