Home arrow Ruby-on-Rails arrow Page 4 - Setting up Posting for a Forum Implementation
RUBY-ON-RAILS

Setting up Posting for a Forum Implementation


In this conclusion to a four-part series on implementing a forum with Ruby on Rails, we'll finish setting up the part of the forum that allows users to view entries, and add the sections that let them make and reply to 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 25, 2010
TABLE OF CONTENTS:
  1. · Setting up Posting for a Forum Implementation
  2. · Implementing the View Post User Story
  3. · Modifying the Controller
  4. · Implementing the Reply to Post User Story

print this article
SEARCH DEVARTICLES

Setting up Posting for a Forum Implementation - Implementing the Reply to Post User Story
(Page 4 of 4 )

After, for example, Jill has posted a message to the forum, George should be able to view the post and reply to it. George should be able to do this by clicking the Reply link on the post details page (see Figure 6-5).

The Reply to Post user story requires that you change the integration test one last time. Add the reply_to_post method to the DSL:

  def reply_to_post(post, parameters)
    get "/forum/reply/#{post.id}"
    assert_response :success
    assert_tag 'h1', :content => "Reply to '#{post.subject}'"

    post "/forum/create/#{post.id}", parameters

    assert_response :redirect
    follow_redirect!
    assert_response :success
    assert_template 'forum/index'
    assert_tag :a, :content => post.subject
  end

The reply_to_post method simulates a user browsing to the reply to post page. The test verifies that the page title is correct and checks that there is a redirection to the forum main page, which indicates that the post was saved successfully. At the end, the test verifies that the main page lists the post that was created, by looking for an anchor tag having the same content as the posts subject.

Next, change the test_forum method as follows:

  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!'
    }
    george.view_forum
    jill.view_forum

    george.view_post post
    george.reply_to_post(post, :post => {
        :name => 'George',
        :subject => 'Rats!',
        :body => 'Rats!!!!!!!!'
     
}
    )
 
end   

This will create a reply to the post Jill made earlier in the test. But when the integration test is run, it should fail with the following error message:

--------------------------------------------
expected tag, but no tag found matching {:content=>"Reply to 'Downtime'", :tag=>"h1"
--------------------------------------------

The test is failing because the reply_to action is empty, when it should be creating a new post and setting the correct page title. Fix this by modifying the reply action:

  def reply
   
reply_to = ForumPost.find(params[:id])
    @page_title = "Reply to '#{reply_to.subject}'"
   
@post = ForumPost.new(:parent_id => reply_to.id)
   
render :action => 'post'
  end

First, the reply_to action loads the post object that the user is replying to from the database. This is because we want the page title to be the subject of the post we are replying to, which is done by setting the page_title instance variable. Then we create a new post object, which is the reply, and set the parent_id to be the id of the post to which we are replying.

Execute the test one final time, and you should see it pass without any errors.

As usual, do a quick manual test by replying to one of the posts you have already created. This opens a URL pointing to http://localhost:3000/forum/reply/1 (note that your id parameter might be different) in your browser, as shown in Figure 6-6.


Figure 6-6.  The reply to post page

After you reply to the post, you should be redirected to the main page, where you should see something similar to Figure 6-7.


Figure 6-7.  The forum main page showing a discussion thread

We notice that George has come up from the basement and that he is holding a Smith & Wesson, just as we suspected. He yells, There's nothing that beats a Smith & Wesson--not even a rabid sewer rat!"

We ask him to do an acceptance test of the forum functionality. He seems happy with how easy it is to use, and tells us that we have done a good job.

Summary

In this chapter, we showed you how to implement a forum with the help of a third-party plugin. We also demonstrated how to create integration tests that simulate multiple users accessing the forum and how to create a view helper to display the posts in the forum as threads.

In the next chapter, you will learn how to implement tagging.


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