Home arrow Ruby-on-Rails arrow Page 4 - 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 - Modifying the Model
(Page 4 of 4 )

The ForumPost model created by the generate script is almost complete. We just need to activate the plugin and add some basic validations to the model to prevent bad data from being stored in the database. We will add validations for three fields:

  1. The name field is used for storing the name of the person who created the post, and must be between 2 to 50 characters in length.
  2. The subject field is shown on the forum main page and should be descriptive but not too long, which is why it is limited to between 5 and 250 characters. 
     
  3. The body field should be long enough for the user to write a short message, but not too long, which is why it is limited to 5000 characters.

We activate the plugin by adding the line acts_as_threaded to our model, and use the validates_length_of validation method to enforce the length of a field. Here are the additions to make to app/models/forum_post.rb :

class ForumPost < ActiveRecord::Base
 
acts_as_threaded

  validates_length_of :name, :within => 2..50
  validates_length_of :subject, :within => 5..255
 
validates_length_of :body, :within => 5..5000
end

Notice how simple Rails and the acts_as_threaded plugin have made it to implement a forum. The model contains only four lines of code, and we needed to create only one table. This is enough for a basic forum implementation. You can now start using the ForumPost model in your controllers to add, retrieve, edit, and delete forum posts.

Please check back tomorrow for the second part of this article. 


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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