Posted over 3 years ago by Philip Ingram
Well, I’m going to try something a little different with this post. I’ll just post some code and explain one or two things that are out of the ordinary, instead of going through a full blow by blow account of it all.
I basically needed to manage my comments and the resulting spam that is associated with having a blog on the interwebs. I haven’t created one as of yet, so here it is.
Simply put, i used my existing Admin Namespace to include a Comments Controller which listed all of the spam, ham and moderate comments available to me, through acts_as_snook .
Now, acts_as_snook has these methods which can update a Comment record from one status to another. E.g., spam to ham. Sometimes acts_as_snook can’t decide whether it’s spam or ham, so it goes into a moderate status and this is when those methods really shine.
Fix your routes by adding:
map.namespace :admin do |admin|
admin.resources :comments, :member => {:ham_it => :get, :spam_it => :get}
end
Next create an Admin::CommentsController and add:
class Admin::CommentsController < ApplicationController
before_filter :get_comment, :except => :index
def index
@spam = Comment.spam
@ham = Comment.ham
@moderate = Comment.moderate
end
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to(admin_comments_url) }
format.xml { head :ok }
end
end
#change to ham
def ham_it
@comment.ham!
respond_to do |format|
format.html { redirect_to(admin_comments_url) }
format.xml { head :ok }
end
end
#change to spam
def spam_it
@comment.spam!
respond_to do |format|
format.html { redirect_to(admin_comments_url) }
format.xml { head :ok }
end
end
protected
def get_comment
@comment = Comment.find(params[:id])
end
end
Finally we need a view to use, so why not add something like this:
<h1>Ham Comments (the good ones)</h1>
in : ‘Are you sure?’, :method => :delete %> |
Moderate Comments (the iffy ones)
in post # : |
Spam Comments (the bad ones)
in post # : ‘Are you sure?’, :method => :delete %> |
Remember, your Comment model schema may vary, but it’s probably similar.
Enjoy. I didn’t bore you with much other content then what was needed. This approach of course can be used to create any kind of admin moderation in an admin namespace.
Keeping Subscription dates tidy during February and Leap years
Testing ActiveResource - Basic Tutorial
Active Resource to Sinatra DataMapper backend
ArmRest and the tale of the No Schema Scheme
Textmate Ruby 1.9.1 and rvm - the facts
Easiest Postgres Install Ever - Mac Edition
Two Questions to Help Decide Between RDBMS, MongoDB or CouchDB
Testing Rails 3 - Just the Facts
Acts_as_snook - Creating an admin panel
Top 10 Movies of this Decade... Rebuttal
Thinking_Sphinx - Easy Setup Tutorial