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

Modify the show action in app/controllers/forum_controller.rb so that it loads the specified forum post. This is done with a call to ForumPost.find:

  def show
    @post = ForumPost.find(params[:id])
    @page_title = '#{@post.subject}'
  end

As usual, we set the page title using the @page_title variable.

Modifying the View

Change the contents of the app/views/forum/show.rhtml to be as follows:

<dl>
  <dt>Name</dt>
  <dd><%= h @post.name %></dd>
  <dt>Subject</dt>
  <dd><%= h @post.subject %></dd>
  <dt>Body</dt>
  <dd><%= h @post.body %></dd>
</dl>
<%= link_to 'Reply', :action => 'reply', :id => @post %> |
<%= link_to 'Back', :action => 'list' %>


Note  It is extremely important that you escape all data that is entered by users with the h method. This helps in protecting you from security risks, such as cross-site scripting attacks. For more information about how to protect your site against various attacks, refer to Chapter 8, Security.

 


 

With the action and view in place, the integration test should now work, so run it once more to verify that the integration test passes:

$ ruby test/integration/forum_test.rb

--------------------------------------------
Loaded suite test/integration/forum_test Started
.
Finished in 0.145623 seconds.

1 tests, 11 assertions, 0 failures, 0 errors
--------------------------------------------

You should also perform a manual test to verify that the page works correctly. Open http://localhost:3000/forum/show/1 (you might have to change the id), and you should see the details of the post, as shown in Figure 6-5. Notice that there is a Reply link at the bottom of the page.


Figure 6-5.  The final version of the post page

The Reply to Post user story is the last user story that we need to implement.

In case you want to display links to replies (to the current post) on the Show Post page, you can add the following code to the view:

<%= display_as_threads @post.direct_children%>

This displays the replies to the current post, but only the direct replies. To display all replies use the following code:

<%= display_as_threads @post.all_children%>


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