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).
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> <p> <%= display_as_threads @posts %> </p> <% else %> There are no posts yet. <%= link_to 'Be the first one to post here' , ? :action => 'post' %> <% end %> <br/>
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:
Change the index action in the controller to retrieve a paginated list of posts.
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.