iie labs

Acts_as_snook - Creating an admin panel

Posted almost 5 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.

Creating an Admin Panel for Acts As Snook

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.

The stone cold facts of what i did

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.

The code…

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.

Conclusion

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.

Comments

blog comments powered by Disqus