Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Message Forum: Message Controller and Message Views - Ruby on Rails

Chapter: Internet & World Wide Web HOW TO PROGRAM - Rich Internet Application Server Technologies - Ruby on Rails

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Message Forum: Message Controller and Message Views - Ruby on Rails

The MessagesController (Fig. 24.38) is similar to the ForumsController, except that its list method (lines 8–22) doesn’t list all of the messages—it lists only the ones with the specified forum_id that is passed in as a URL parameter from the Forum list view.

Message Controller and Message Views

 

The MessagesController (Fig. 24.38) is similar to the ForumsController, except that its list method (lines 8–22) doesn’t list all of the messages—it lists only the ones with the specified forum_id that is passed in as a URL parameter from the Forum list view. Line 10 updates the session variable to match the URL parameter. If no parameter value is specified, the forum_id session variable is used. If neither of these exists, line 14 displays an error and line 15 redirects the client to the list action of the forum controller. The find method which is called on Message (line 18–19) specifies that the messages should be ordered by their created_on dates in descending order. Line 20 also calls the find method to obtain the forum object, which we will need to add messages to the forum.

 

The create method (lines 30–40) replaces the method generated by the scaffold. Line 31 obtains the id of the forum to which the message should be added and line 32 obtains the new message entered by the user. If the message is added to the database successfully, lines 35–36 set the appropriate message to be displayed in the view using the flash object and redirect the client to the list action. Otherwise, line 38 redirects the client to the new action, prompting the user to enter the message again.

 

1    # Fig. 24.38: app/controllers/messages_controller.rb

 

2    # MessagesController that implements CRUD functionality.

 

3    class MessagesController < ApplicationController

 

4          verify :method => :post, :only => [ :destroy, :create ],

 

5                 :redirect_to => { :action => :list }

6

7          # sets up the list web page that lists all messages

 

8          def list

 

9                 if ( params[ :forum_id ] )  # if parameter forum_id is provided

 

10                      session[ :forum_id ] = params[ :forum_id ]

 

11                end # if

12

13                if ( session[ :forum_id ] == nil ) # if no forum_id is provided

 

14                      flash[ :notice ]  = 'there has been an error.'

 

15                       redirect_to :controller => "forums", :action => "list" and return

 

16                end # if

17

18                @messages = Message.find( :all, :order => "created_on desc",

 

19                      :conditions => "forum_id = #{ session[:forum_id ] }" )

 

20                @forum = Forum.find( :first,

 

21                      :conditions => "id = #{ session[ :forum_id ] }" )

 

22         end # method list

23

24         # sets up the new web page that creates a message

 

25         def new

 

26                @message = Message.new

 

27         end # method new

28

29         # attempts to create a new message with the parameters passed in

 

30         def create

 

31                @message = Message.new( params[ :message ] )

 

32                @message.forum_id = session[ :forum_id ]

33

34                if @message.save  # if save method was successful

 

35                      flash[ :notice ] = 'Message was successfully created.'

 

36                      redirect_to :action => 'list'

 

37                else # save was unsuccessful

 

38                      render :action => 'new'

 

39                end # if

 

40         end # method create

 

41   end # class MessagesController

 

Fig. 24.38 | MessagesController that implements CRUD functionality.

 

List View

The list view (Fig. 24.39) for the Message controller is similar to the list view for the Forum controller, except that more information is displayed in the messages list view. It uses CSS to format the output. In this view, every message object acts like a Hash—passing a column name as a key returns the corresponding value in the message object. To obtain an a column’s value, include the attribute method’s name in square brackets after the name of the object. For each message in the forum, line 12 displays the title, line 13 displays the author and line 19 displays the message’s text. At line 14, the Ruby Time object that is returned by the message[’created on’] is formatted using the Ruby Time class formatting

 

1     <!-- Fig. 24.39: app/views/messages/list.rhtml -->

 

2     <!-- Template for the list action that displays a list of Forums. -->

 

3     <div style = "text-align: center">

 

4           <table style = "width: 600px; margin: 0 auto 0 auto">

 

5                  <tr class="msgHeader">

 

6                        <td><%= @forum.name %></td>

 

7                  </tr>

 

8                  <% for message in @messages %>

 

9                        <!-- create two table rows for every message -->

 

<tr class="msgTitle">

11        <td>

12        <strong><%= message[ 'title' ] %></strong><br />

13        by <em><%= message[ 'author' ] %></em> at

14        <%= message[ 'created_on' ].strftime("%m/%d/%Y at %I:%M%p") %>

15                              </td>

 

16                       </tr>

 

17                       <tr class="msgPost">

18                      <!-- message content -->

 

19                              <td><%= message[ 'message' ] %></td>

 

20                       </tr>

 

21                 <% end %>

 

22          </table>

23

24          <%= link_to 'New message', :action => 'new' %> |

 

25          <%= link_to 'list forums',

 

26                 :controller => 'forums', :action => 'index' %>

27                 </div>


 

Fig. 24.39 | Template for the list action that displays a list of messages.

 

options. Lines 24–26 use the link_to method to allow the user to create a message in the current forum or to go back to the list of the forums.

 

New View

The new template for the Message controller is omitted here because it is scaffold code that is nearly identical to the new template for the Forum controller. The partial shown in Fig. 24.40 for the messages form is also similar. Lines 8, 12 and 16 use the text_field helper method to create fields for specifying the title, author and email. Line 20 uses the text_area helper method to create an input area of a certain size, to be used to input the message. These fields are validated when the Message model’s save method is called. If the model does not deem the data valid, line 3 displays the error messages.

 

1    <!-- Fig. 24.40: app/views/messages/_form.rhtml -->

 

2    <!-- A form that allows the user to enter a new message. -->

 

3    <%= error_messages_for 'message' %>

4

5    <table>

 

6          <tr class = "alignRight">

 

7                 <td>Title</td>

 

8                 <td><%= text_field 'message', 'title'  %></td>

9          </tr>

 

10         <tr class = "alignRight">

 

11                <td>Author</td>

 

12                <td><%= text_field 'message', 'author' %></td>

 

13         </tr>

 

14         <tr class = "alignRight">

 

15                <td>Email</td>

 

16                <td><%= text_field 'message', 'email'  %></td>

 

17         </tr>

 

18         <tr><td colspan = "2">Message</td></tr>

 

19         <tr><td colspan = "2">

 

20                <%= text_area 'message', 'message', :cols => "30", :rows => "4"%>

 

21         </td></tr>

 

</table>


Fig. 24.40 | Form that allows the user to enter a new message.

 

 

Message Layout

Figure 24.41 shows the layout used to render all Message templates. Line 10 invokes the scaffold.css style sheet, which we changed slightly to improve our page’s presentation. To make the style sheet available for import it must be placed in the public/stylesheets directory of the application. When a forum has been modified, line 14 displays the appro-priate message.

 

1    <?xml version = "1.0" encoding = "utf-8"?>

 

2    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 

3          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

4              

5    <!-- Fig. 24.41: app/views/layouts/messages.rhtml -->

 

6     <!-- Message layout that links a style sheet and displays a message. -->

 

7    <html xmlns = "http://www.w3.org/1999/xhtml">

 

8    <head>

 

9          <title>Messages: <%= controller.action_name %></title>

 

10         <%= stylesheet_link_tag 'scaffold' %>

 

11   </head>

 

12   <body>

 

13         <% if ( flash[ :notice ] ) then %>

 

14                <p style="color: green"><%= flash[ :notice ] %></p>

 

15         <% end %>

 

16         <%= yield %>

 

17   </body>

 

18   </html>

 

Fig. 24.41 | Message layout that links a style sheet and displays a message.

 

We have now implemented the basic functionality of the forum application. To test this application execute it on Mongrel and browse to  http://localhost:3000/forums.

 

To test the administrative privileges of the forum go to  http://localhost:3000/user/ admin and login with the username user1 and password 54321. In the next section we’ll add Ajax capabilities to make our forum more responsive.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.