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

 Modifying the Controller

Start by changing the index action. Open app/controllers/forum_controller.rb in an editor and modify the index action as follows:

  def index
   @page_title = 'Forum'
    @post_pages, @posts = paginate :forum_posts, :per_page => 20, ?
  :order => 'root_id desc, lft'
 
end

The paginate method is used to retrieve a paginated list of ForumPost objects. Furthermore, we specify that we want to show a maximum of 20 posts per page, and that the posts should be ordered by the root_id and the lft columns.

You can try to access the page again in your browser or run the integration test. Both should fail with an error being shown, because the display_as_threads method is not found.

Using a View Helper

Displaying a list of posts requires a fair amount of code, as each post can have one or more replies, and these need to be shown as a thread. To avoid cluttering the view with too much Ruby code, we will use a view helper to generate the list of posts. 


Tip  Putting too much code in the view is usually a bad idea. If you see that your view is getting cluttered with Ruby code, you can refactor it and move the code to a view helper, controller, or another part of the system. 


The generate script already created a view helper for you. Open app/helpers/forum_helper.rb and add the display_as_threads method, as follows:

module ForumHelper
  
def display_as_threads(posts)
    content = ''
    for post in posts
      url = link_to("#{h post.subject}", {:action => 'show', :id => post.id})
      margin_left = post.depth*20
      content << %(
      <div style="margin-left:#{margin_left}px">
       
#{url} by #{h post.name} ?
#{post.created_at.strftime("%H:%M:%S %Y-%M-%d") }
 
      </div>
   
end
    content
  end
end  

The new display_as_threads method takes an array of post objects as a parameter. Recall that we are calling this method from the view and passing the posts variable to it, which is initialized by the controller. The method loops through all the posts and creates the HTML necessary for displaying the posts in a threaded fashion.

Note that each post is located inside a div tag that is indented to the right by specifying a left margin. This is done with the margin-left CSS property. The size of the indentation is related to how deep down the reply is in the hierarchy; each reply is indented 20 pixels to the right of the parent. Also note that the link_to method is used to create a link to the show post page, which we will implement in the next section.

Open your browser and create a couple of posts. You should see a page similar to the one shown in Figure 6-4. Recall that in Chapter 2, we changed the layout to show the flash message at the top of the page.  

 
Figure 6-4.  The forum main page

You can now run the test again by executing ruby test/integration/forum_test.rb . You should see the test pass without errors or failures.

$ ruby test/integration/forum_test.rb

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

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

You can now both create and view a list of posts. The next logical thing to implement is the View Post user story.  


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-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials