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).
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:
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.
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.
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
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.