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).
In this chapter, we will show you how to implement a forum that can be used by Emporium's customers to discuss book-related topics and provide feedback to George. Gathering feedback from customers is an important part of an e-commerce site, as it allows you to adapt and improve your service. For example, before implementing a new feature, you could post a message to the forum, asking your customers if they would use the feature. If the answer is no, you will save both money and time. Other uses for a forum include allowing customers to post bug reports and review books, for example.
Implementing a forum can be time-consuming. To save us some time, and save George some money, we will use the acts_as_threaded plugin as the basis for the forum implementation. While implementing the forum, we will also show you how to use view helpers. View helpers are a built-in feature of Rails that allow you to keep your views clean from excessive Ruby code. As usual, we will use TDD while implementing the forum, to ensure that the forum is properly tested and working according to the requirements. And as in previous chapters, we will use integration testing. In this case, we will simulate multiple users accessing the forum and posting to the forum at the same time.
Getting the Forum Requirements
We start the new day in our humble cubicle, by arranging a meeting with George to discuss the requirements for the feedback forum. George tells us that he is excited about the new possibilities the forum will offer. He yells, "It has to be simple to use. I must also be able to use the forum!" We show him a sketch of the user interface as we envision it. The create post page has three fields: name, subject, and body. George jokes, "Looks like I will be able to use that." We tell him this is as simple as it gets, and that each discussion in the forum (also referred to as a thread) starts from a root post. Replies to the root post are shown in a threaded fashion, so that it is easy to follow the discussion. We write down the first user stories, View Forum and Post to Forum.
Next, we show George a sketch of the View Post user story. He says, "You guys are professionals. I haven't seen the forum yet, but I can already feel that it will be something special!" We assure him that the forum will be simple and easy to use, and then continue by writing down the View Post user story.
George informs us that we have to end the meeting early. He has just received news from Jill (Emporium's best customer) that the Emporium website is down again. He has to make an emergency call to his IT department, which happens to be his nephew. Before he leaves, we show him a sketch of the Reply to Post user story. He again praises our fine sketches and runs off towards the basement, mumbling, "I really hope that darn rat hasn't put its head through the server's processor fan again. It would be the third time this month." We try not to pay any attention to what he just said and write down the Reply to Post user story.
With George in the basement, we can start the implementation of the forum. Here are the user stories for this sprint:
View forum: Users should be able to list all posts in the forum. Posts belonging to the same discussion should be shown in a threaded fashion, and the list should be sorted, with the most recent posts shown first.
Post to forum: Users should be able to start a new discussion in the forum by entering their name, a subject, and the body text of the post.
View post: Users that are viewing the forum should be able to click on a post and view the details.
Reply to post: A customer views a forum post and decides to reply to the post by clicking the reply link. The user enters his name, the subject, and reply message, and then clicks the reply button.
We'll get started by installing the acts_as_threaded plugin.