Home arrow Ruby-on-Rails arrow Forum Implementation: Viewing Posts
RUBY-ON-RAILS

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
TABLE OF CONTENTS:
  1. · Forum Implementation: Viewing Posts
  2. · Testing
  3. · Implementing the View Forum User Story
  4. · Modifying the View

print this article
SEARCH DEVARTICLES

Forum Implementation: Viewing Posts
(Page 1 of 4 )

Completing the Controller

The controller contains two actions that are used in the Post to Forum user story. One displays the form, and the other takes the user input and persists the post to the database. Both the post and create actions exist already, but they contain no code, so modify app/controllers/forum_controller.rb as follows:

  def post
   
@page_title = 'Post to forum'
   
@post = ForumPost.new
  end

  def create
   
@post = ForumPost.new(params[:post])
   
if @post.save
     
flash[:notice] = 'Post was successfully created.'
     
redirect_to :action => 'index'
   
else
     
@page_title = 'Post to forum'
     
render :action => 'post'
   
end
  end

The post action does one thing: it creates a new ForumPost object that is used by the form tags in the view. The create action is a bit more complex. It receives the form input from the user and creates a new ForumPost object. It then tries to save it to the database. If the user-supplied data passes validation, it redirects the user to the forum main page. If there are any validation errors, it renders the create post page instead, where we use the error_messages_for helper to show the validation errors to the user.

Creating the View

Recall that the Post to Forum user story is implemented with the controller's post action, which directly maps to the app/views/forum/post.rhtml view. The ERB code for this view is shown in Listing 6-4. Save the code in app/views/forum/post.rhtml .

Listing 6-4. View for the Post to Forum User Story

<%= error_messages_for 'post' %>
<%= start_form_tag :action => 'create' %>
    <%= hidden_field :post, :parent_id %>
    <p><label for="post_name">Name</label><br/>
    <%= text_field 'post', 'name'  %></p>
    <p><label for="post_subject">Subject</label><br/>
    <%= text_field 'post', 'subject'  %></p>
    <p><label for="post_body">Body</label><br/>
    <%= text_area 'post', 'body'  %></p>

  <%= submit_tag "Post" %>
<%= end_form_tag %>

The view uses the built-in Ruby on Rails form helpers hidden_field , text_field , and text_area for creating the form fields. Note that we are using label tags and that the label tags for attribute is used to associate the label with a form field. Using label tags in forms is important because it improves usability, such as by extending the clickable area to outside the field. The hidden field in the view is used by the Reply to Post user story, which we will implement later in this chapter.


Tip  For more information about form helpers, see the online documentation at: http://api.rubyonrails.org/classes/ActionView/ Helpers/FormHelper.html .



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