Home arrow Ruby-on-Rails arrow Page 3 - Forum Implementation: Viewing Posts

Forum Implementation: Viewing Posts

In this third part of a four-part series on setting up a forum for an online bookstore using Ruby on Rails, we pick up where we left off; we'll also start setting up the sections that allow visitors to view forum posts. This article 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 / 2
May 24, 2010
  1. · Forum Implementation: Viewing Posts
  2. · Testing
  3. · Implementing the View Forum User Story
  4. · Modifying the View

print this article

Forum Implementation: Viewing Posts - Implementing the View Forum User Story
(Page 3 of 4 )

The View Forum user story describes how a user can access the forum's main page to view a list of the most recent posts. The page displays all the forum posts in a threaded fashion, with the most recent post shown at the top of the list, similar to this example:

Hello from the Cayman Islands by George * 02/02/2006
-->Bring a keg of Rum by Jill * 02/03/2006
---->Is one enough? by George * 02/05/2006 WARNING! Rat poison deployed in server room by George * 31/12/2005

George has great plans for Emporium. He envisions a lot of traffic and users posting exotic questions on the forum, so showing all posts on the same page is not very wise. This is easy to fix with pagination, which we will use to ensure that no more than 20 posts are shown at the same time.

Updating the Integration Test

As explained earlier, integration tests are great for testing multiple sessions. The test that we'll create next will check this by simulating both George and Jill accessing the forum at the same time. Add the following code to the DSL in test/integration/forum_test.rb , immediately after the post_to_forum method:

  def view_forum
    get "/forum"

    assert_response :success
    assert_template "forum/index"
    assert_tag :tag => 'h1', :content => 'Forum'
    assert_tag :tag => 'a', :content => 'New post' 

This method simply accesses the forum main page and verifies that the heading on the page is displayed correctly. The main forum page should also contain a link to the create post page. This is verified with another assert_tag that looks for a link named New post . Change the test_forum method as follows (changes are highlighted):

  def test_forum
    jill = new_session_as(:jill)
george = new_session_as(:george)
post = jill.post_to_forum :post => {
      :name => 'Bookworm',
      :subject => 'Downtime',
      :body => 'Emporium is down again!'

This simulates George opening the forum in his browser right after Jill has posted to the forum. In later tests, we'll create a test that simulates George replying to the post made by Jill.

Next, run the test by executing ruby test/integration/forum_test.rb . You should see the test fail with the following message:

expected tag, but no tag found matching {:content=>"New post", :tag=>"a"} in:

The test is expecting to find the link New post, but fails because we haven't modified the view. Note that the test didn't fail on the first assert because the heading is Forum#index .

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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