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

Fix the view by opening app/views/forum/index.rhtml and changing the contents of the file to match Listing 6-5.

Listing 6-5. The View for the View Forum User Story

<% if @posts.size > 0 %>
<div><%= link_to 'New post' , :action => 'post' %></div>
<%= display_as_threads @posts %>
<% else %>
There are no posts yet. <%= link_to 'Be the first one to post here' , ?
:action => 'post' %>
<% end %>

<%= link_to 'Previous page' , { :page => @post_pages.current.previous } ?
if @post_pages.current.previous %>
<%= link_to 'Next page' , { :page => @post_pages.current.next } ?
if @post_pages.current.next %>

Run the integration test again with the following command: $ ruby test/integration/forum_test.rb . This time, you should see the test fail with the following message:

Expected response to be a <:success>, but was <500>

To find the cause for this, open your browser and go to http://localhost:3000/forum . As shown in Figure 6-3, the page shows a nice and detailed error message of where the error is located.  

Figure 6-3.  Rails error page

Tip  The error page that is displayed when exceptions are thrown is exceptionally well thought out. It shows you details about the request, response, and session. At the bottom of the page, you can also see the stack trace leading up to the error, including links that allow you to filter the stack trace to show only your application code, the Rails framework's code, or the full trace.

The error page tells you that line 1 contains an error and shows you the code around line 1 in your browser. As you can see, the page generates an error when it tries to call thesize method on the @posts instance variable. This is because we haven't initialized the @posts variable in the controller. Fixing the page requires completing the following tasks:

  1. Change the index action in the controller to retrieve a paginated list of posts.
  2. Create a view helper containing the display_as_threads method. This method is used to display the forum posts.

Please check back tomorrow for the conclusion to 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 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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials