Home arrow Ruby-on-Rails arrow Page 2 - Forum Implementation
RUBY-ON-RAILS

Forum Implementation


Setting up a forum is a great way to keep your customers interested and get feedback on what you're doing. This article, the first part of a four-part series, will show you how to set one up for an online bookstore using Ruby on Rails. It is excerpted from chapter six of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
May 20, 2010
TABLE OF CONTENTS:
  1. · Forum Implementation
  2. · Using the Threaded Forum Plugin
  3. · Setting Up the Forum
  4. · Modifying the Model

print this article
SEARCH DEVARTICLES

Forum Implementation - Using the Threaded Forum Plugin
(Page 2 of 4 )

The acts_as_threaded plugin was originally developed by Bob Silva and allows you to easily implement a forum. As you've seen in previous chapters, plugins are used by developers to extend the core functionality provided by the Ruby on Rails framework. Implementing a forum with the acts_as_threaded plugin is as simple as most tasks in Rails. Install the plugin, and then add a database table, model, controller, and view.

The complex logic for storing forum data in a relational database, like MySQL, is handled by the plugin, allowing you to concentrate on implementing the forum, instead of writing infrastructure code. By not reinventing the wheel, you can implement the forum faster and probably with fewer bugs than if you implemented it yourself, which means a happier client in the end.

Download the plugin installation package from www.railtie.net/plugins/acts_as_threaded.zip.
Then extract the package to the vendor/plugins directory, which, in our case, is /home/george/projects/emporium/vendor/plugins.
Lastly, verify that you have the following files in the vendor/plugins/acts_as_threaded directory:

  1. vendor/plugins/acts_as_threaded
  2. vendor/plugins/acts_as_threaded/init.rb 
     
  3. vendor/plugins/acts_as_threaded/lib/threaded.rb

The init.rb file is called by Rails on startup and initializes the plugin. The threaded.rb file contains the actual plugin code, which we will use to implement the forum.

Activating the plugin is as simple as adding a call to the acts_as_threaded method to your model, as in this example:

class Post < ActiveRecord::Base  
  acts_as_threaded
end

The acts_as_threaded plugin adds the instance methods listed in Table 6-1 to the model.

Table 6-1. Instance Methods Added by the acts_as_threaded Plugin

Method Description
post.root? Returns true if the post is the root post in the thread
post.child? Returns true if the post is the child of another post
post.add_child(child) Adds a reply to the post
post.children_count Returns the number of replies under this post
post.full_set Returns an array containing the post itself and all replies under it
post.all_children Returns an array containing all replies
post.direct_children Returns an array containing only replies to this post

 

 

 

 

 

 

 

 

 

 

We can hear George swearing loudly in the basement, "Damn sewer rat, you never learn! Do you? This is your last... ." Then we hear a gun of some sort being fired six times. "Was that a Smith & Wesson? we ask ourselves, before continuing with the forum implementation.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials