Building Dynamic Web 2.0 Websites with Ruby on Rails

SHARE & COMMENT :

Building Dynamic Web 2.0 Websites with Ruby on Rails

Ruby on Rails is an open-source web application framework ideally suited to building business applications, accelerating and simplifying the creation of database-driven websites. It has been developed on the Ruby platform.

This book is a tutorial for creating a complete website with Ruby on Rails (RoR). It will teach you to develop database-backed web applications according to the Model-View-Controller pattern. It will take you on a joy ride right from installation to a complete dynamic website. All the applications discussed in this book will help you add exciting features to your website. This book will show you how to assemble RoR’s features and leverage its power to design, develop, and deploy a fully featured website.

What This Book Covers

Chapter 1 gives you an overview of the features of Ruby and RoR, as well as providing the various ways of installing, configuring, and testing both Ruby and RoR.

Chapter 2 introduces you to the basics of Ruby as well as the main concepts and components of RoR.

Chapter 3 makes you understand the design of tables according to the conventions of RoR, creation of scaffolds for tables, and changing the scaffolds according to the requirements.

Chapter 4 gives you details about how to set up the User Management module for the website called TaleWiki.

Chapter 5 makes you familiar with the Login Management and Comment Management modules for TaleWiki.

Chapter 6 introduces you to the Migrations and Layouts involved in setting up the template for TaleWiki.

Chapter 7 describes the tagging functionality being implemented for the enhanced search usability.

Chapter 8 provides you with the implementation of AJAX for TaleWiki.

Chapter 9 deals with the development of an interface for the administration.

Chapter 10 gives you the steps for deploying the website.

Gathering User Comments

In the last chapter, we saw how to set up User Management and Role Management for TaleWiki. However, we did not set up the Login Management based on Users. So, it was work only half done. To complete the task, we will set up Login Management in this chapter. It will not only authenticate a user but also provide the session management.

Secondly, we will look at how to gather user comments for a particular story. We will start with the functionalities to be provided by the Comment Gathering module. We will then move on to the database design for the module. After that we will not only set up the Login Management but also modify the Tale Management so that the User and Tales can be related. We will wrap up with the implementation of the Comment Gathering module. Let’s gets started.

Understanding the Requirements

In this chapter, we will be tackling two problems—managing the user authentication as well as the session management and accepting comments from other users for a particular tale. So we can divide the requirements into two:

  • Login Management
  • Comment management

The Login Management module will also provide the solution to the problem of Tale management that evolved during the development of User management. As the tales table refers to the users table, without a user id a new tale cannot be submitted. The Login management will provide us the user id corresponding to the new tales. Also, it will tell us who has commented on a particular tale. Let us see how.

Login Management

As the name suggests, the main functionality the Login Management will provide will be managing the logins. However, managing logins is not a single task. It is dependent on others tasks or operations as well. So, the overall functionalities we will be developing as part of Login management are:

  • Authenticating the User: We can allow only the registered users to access the functionalities of TaleWiki. This operation will ensure that the user is a registered user before he or she tries to enter the TaleWiki.
  • Setting the Session: Once the user is found to be authentic, then we have to maintain his/her authenticity until he/she logs out. The authenticity can be maintained by this functionality.
  • Checking Roles: Each User is assigned a Role. So we will need to check whether a particular functionality—such as viewing details of another user—is a part of the Role. This functionality will check the User’s Role whenever he/she tries to access any functionality provided by TaleWiki.
  • Invalidating Session: When a user logs out, all the details of the user in the current session need to be cleared out. This functionality will clear out all the details of the user, including whether the user is authentic or not.

Now that we have defined the functionalities of Login management, let us move on to the next set of tasks—managing the comments.

Managing the Comments

It is natural for a person to comment upon whatever he or she reads. So, it is necessary to provide a way for users to comment on a particular story. The comments can be of two types—threaded and non-threaded. In threaded comments, one comment can be posted as a response for another comment. If the first comment is removed, then all its child comments will also be removed. If we go for non-threaded
comments, then each comment is considered an individual. So if one is deleted, others are not affected.

The Comment Management module will do the same. The functionalities that the Comment Management module will provide are:

  • Adding a Comment: When a user wants to comment on a particular story, he or she can use this functionality. A user can comment on many stories. Comments are not threaded. That means a comment cannot be a response for another comment. Each comment is considered an individual.
  • Deleting a Comment: If an administrator finds a comment offensive or feels that comments are very old, this functionality can be used to delete such comments. Only the administrator will have access to this functionality.
  • Viewing Comments: Using this functionality, a user can read all the comments submitted for a particular story. It will be available for all users. In addition, the comments will be shown in the list view and the details view. In list view, the comments will be shown for each story, and in the details view, all the details including the date and complete text of the comment will be shown.

We are not providing a way to modify a posted comment. That is because comments are considered one time and brief view of what the user thinks. Hence, no functionality will be provided for the modification of comments. That wraps up the requirements of the Login and Comment Management modules. Next, let us work on the database design for the modules.

Designing the Database

As you would have already guessed, our next step will be designing the database. However, unlike the modules that we developed previously, we will be designing the database only for one of the two modules. The Login management module doesn’t require a table because its functionalities are based on the users and roles tables. So we will have to design the table for the Comment management module only. Just like the previous chapter, the steps for designing the database are:

  • Designing the E-R Model
  • Deriving the Schemas
  • Creating the Tables

Whenever a new module is added, some of the existing E-R models need to be refined, and consequently the corresponding schemas and tables will be changed accordingly. In the case of Comment management, this holds true as you will see as we go through the steps. So here we go.

Designing the E-R Model

As the name suggests, the Comment Management module will have data related to the comments submitted by the user. What is this data apart from the comment itself? To answer this, first let us try to define the functionality of the Comment Management module in one line. ‘Comment management will manage comments submitted by a user for a particular story’—that’s how what will look like. The important point here is ‘comments submitted by a user for a particular story’. We have three main entities—Comments, Users, and Stories. Story and User entities have already been discussed in Chapters 3 and 4. So let us look at the Comments entity. The attributes for comments will include the date on which the comment has been added and the title of the comment. In short, the Comments entity will have the following attributes:

  • Id—the unique number to identify each comment
  • Comment body—the text of the comment
  • Date—the date on which comment was added
  • User—the user who has added the comment
  • Story—the story on which the comment has been made

The entity diagram for the Comments entity will be as follows:

Coming back to our one line definition, we know that the User, Story, and Comments entities are related. The question is how are they related? The answer is there in the one line definition itself. First, let us consider the User entity. The definition says ‘comments submitted by a user’. That means one user can submit many comments. Hence, the User entity has a one-to-many relationship with the Comments entity. The relationship will be as follows in terms of an E-R diagram:

The next part of the definition tells us ‘comments for a story’. This means that one story can have many comments. In other words, the Comments entity is related to the Story entity through a many-to-one relationship. The Story entity will be at the ‘one’ end and the Comments entity will be at the ‘many’ end of the relationship. The diagram will look like as follows:

When looking at all the entities with their attributes and relationships, the picture will be as follows:

The next step obviously is deriving the schema. Here it comes.

Deriving the Schema

We have the complete information about the attributes and relationships of the Comments entity. The main point about this entity is that unlike the User entity it doesn’t introduce any changes in the existing schemas. The reason is that the Comment entity is dependent on other entities and not vice versa. The schema will be as follows:

Here Story and User both have their own schemas. So their Ids will be the foreign keys in the table. Now, we can develop the table.

Creating the Tables

There is only one table to be created. Apart from the attributes, the comments table (keeping with the naming convention), will have two foreign key references—one to the users table and another to the tales table. Including these, the SQL query will be as follows:

	CREATE TABLE `comments` (
	`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
	`comment_body` TEXT NOT NULL ,
	`submission_date` DATE NOT NULL ,
	`tale_id` INT NOT NULL,
	`user_id` INT NOT NULL,
	CONSTRAINT `fk_comments_users` FOREIGN KEY (`user_id`) REFERENCES
	users( `id`) ,
	CONSTRAINT `fk_comments_tales` FOREIGN KEY (`tale_id`) REFERENCES
	tales( `id`)
	) ENGINE = innodb;

That completes the table definition. By this time you will have started to think that if RoR is so productive, why do we still have to use the SQL statements to create tables?. There is another way—the Ruby way. We will see that in the next chapter where we will convert all the table creation statements using Ruby. Now that the required table has been defined, let us develop the modules starting with the Login management.

Developing the Login Management Module

Even though Login and session handling are separate functionalities from User management, they depend on the same table—user. Also, the functionalities are more alike than different. Hence, instead of creating a new Controller, we will be using the UserController itself as the Controller for the Login module. Keeping this point in mind, let us look at the steps involved in developing the Login Management, which are:

  • Creating the Login page
  • Implementing the Authentication Method
  • Setting up the Session
  • Applying Authorization

Leaving aside the first step, all other steps mainly focus on the Controller. Here we go.

Creating the Login Page

We need a login page with textboxes for user name and password in which users can put their credentials and submit to the login authenticator (fancy name for the action method that will contain the logic to authenticate the user). That’s what we are going to create now. The convention for any website is to show the login page when the user enters the URL without any specific page in mind. RoR also follows this convention.
For example, if you enter the URL as http://localhost:3000/user, it displays the list of users. The reason is that the index action method of the UserController class calls the list method whenever the aforementioned URL is used. From this, we can understand two things—first, the default action method is index, and second, the first page to be shown is changeable if we change the index method.

What we need is to show the login page whenever a user enters the URL http://localhost:3000/user. So let’s change the index method. Open the user_controller.rb file from the app/views/user folder and remove all the statements from the body of the index method so that it looks like as follows:

	def index
	end

Next, let us create an index.rhtml file, which will be shown when the index method is called. This file will be the login page. In the app/views/user folder, create an index.rhtml file. It will be as follows:

	<%= form_tag :action=> 'authenticate'%>
	<table >
		 <tr align="center" class="tablebody">
			<td>User name:</td>
			<td><%= text_field("user", "user_name",:size=>"15" ) %></td>
		</tr>
		<tr align="center" class="tablebody">
			<td>Password:</td>
			<td><%= password_field("user",
				"password",:size=>"17" ) %></td>
		</tr>
		<tr align="center" class="tablebody">
			<td></td>
			<td><input type="submit" value=" LOGIN " /></td>
		</tr>
	</tabale>

It uses two new form helpers—text_field and password_field. The text_field creates a text field with the name passed as the parameter, and the password_field creates a password field again with the name passed as the parameter. We have passed the authenticate method as the action parameter so that the form is submitted to the authenticate method. That completes the login page creation. Next, we will work on the authenticate method.

Implementing the Authenticate method

Authenticating a user essentially means checking whether the user name and password given by the user corresponds to the one in database or not. In our case, the user gives us the user name and password through the login page. What we will be doing is checking whether the user is in database and does the password that we got corresponds to the password stored in the database for the user? Here, we will be working on two levels:

  • Model
  • Controller

We can put the data access part in the action method that being the Controller itself. But it will create problems in the future if we want to add something extra to the user name/password checking code. That’s why we are going to put (or delegate) the data access part into Model.

Model

We will be modifying the User class by adding a method that will check whether the user name and password provided by the user is correct or not. The name of the method is login. It is as follows:

	def self.login(name,password)
		find(:first,:conditions => ["user_name = ? and password =
			?",name, password])
	end

It is defined as a singleton method of the User class by using the self keyword. The singleton methods are special class-level methods. The conditions parameter of the find method takes an array of condition and the corresponding values. The find method generates an SQL statement from the passed parameters. Here, the find method finds the first record that matches the provided user_name and password. Now, let us create the method that the Controller will call to check the validity of the user. Let us name it check_login. The definition is as follows:

	def check_login
		User.login(self.user_name, self.password)
	end

This function calls the login method. Now if you observe closely, check_login calls the login function. One more point to remember—if a method ‘test’ returns a value and you call ‘test’ from another method ‘test1,’ then you don’t need to say ‘return test’ from within ‘test1′.The value returned from ‘test’ will be returned by ‘test1′ implicitly. That completes the changes to be done at the Model level. Now let us see the changes at the Controller-level.

Controller

In the Controller for User—UserController—add a new method named authenticate. The method will first create a User object based on the user name and password. Then it will invoke check_login on the newly created User object. If check_login is successful, that is, it does not return nil, then the user is redirected to the list view of Tales. Otherwise, the user is redirected to the login page itself. Here is what the method will look like:

	def authenticate
		@user = User.new(params[:user])
		valid_user = @user.check_login
		if logged_in_user
			flash[:note]="Welcome "+logged_in_user.name
			redirect_to(:controller=>'tale',:action => "list")
		else
			flash[:notice] = "Invalid User/Password"
			redirect_to :action=> "index"
		end
	end

The redirect_to method accepts two parameters—the name of the Controller and the method within the Controller. If the user is valid, then the list method of TaleController is called, or in other words, the user is redirected to the list of tales. Next, let us make it more robust by checking for the get method. If a user directly types a URL to an action, then the get method is received by the method. If any user does that, we want him/her to be redirected to the login page. To do this, we wrap up the user validation logic in an if/else block. The code will be the following:

	def authenticate
		 if request.get?
			render :action=> 'index'
		else
			@user = User.new(params[:user])
			valid_user = @user.check_login
			if valid_user
				flash[:note]="Welcome "+valid_user.user_name
				redirect_to(:controller=>'tale',:action => 'list')
			else
				flash[:notice] = "Invalid User/Password"
				redirect_to :action=> 'index'
			end
		end
	end

The get? method returns true if the URL has the GET method else it returns false. That completes the login authentication part. Next, let us set up the session.

In Ruby, any method that returns a Boolean value—true or false—is suffixed with a question mark (?). The get method of the request object returns a boolean value. So it is suffixed with a question mark (?).

Setting up the Session

Once a user is authenticated, the next step is to set up the session to track the user. Session, by definition, is the conversation between the user and the server from the moment the user logs in to the moment the user logs out. A conversation is a pair of requests by the user and the response from the server. In RoR, the session can be tracked either by using cookies or the session object. The session is an object provided by RoR. The session object can hold objects where as cookies cannot. Therefore, we will be using the session object. The session object is a hash like structure, which can hold the key and the corresponding value. Setting up a session is as easy as providing a key to the session object and assigning it a value. The following code illustrates this aspect:

	def authenticate
		if request.get?
			render :action=> 'index'
		else
			@user = User.new(params[:user])
			valid_user = @user.check_login
			if valid_user
				session[:user_id]=valid_user.id
				flash[:note]="Welcome "+valid_user.user_name
				redirect_to(:controller=>'tale',:action => 'list')
			else
				flash[:notice] = "Invalid User/Password"
				redirect_to :action=> 'index'
			end
		end
	end

That completes setting up the session part. That brings us to the last step—applying authorization.

Applying Authorization

Until now, we have authenticated the user and set up a session for him/her. However, we still haven’t ensured that only the authenticated users can access the different functionalities of TaleWiki. This is where authorization comes into the picture. Authorization has two levels—coarse grained and fine grained. Coarse grained authorization looks at the whole picture whereas the fine grained authorization looks at the individual ‘pixels’ of the picture. Ensuring that only the authenticated users can get into TaleWiki is a part of coarse grained authorization while checking the privileges for each functionality comes under the fine grained authorization. In this chapter, we will be working with the coarse grained authorization.

The best place to apply the coarse grained authorization is the Controller as it is the central point of data exchange. Just like other aspects, RoR provides a functionality to easily apply any kind of logic on the Controller as a whole in the form of filters. To jog your memory, a filter contains a set of statements that need to be executed before, after (or before and after) the methods within the Controllers are executed.

Our problem is to check whether the user is authenticated or not, before any method in a Controller is executed. The solution to our problem is using a ‘before filter’. But we have to apply authorization to all the Controllers. Hence, the filter should be callable from any of the Controller. If you look at the definition of a Controller, you can find such a place. Each Controller is inherited from the ApplicationController. Anything placed in ApplicationController will be callable from other Controllers. In other words, any method placed in ApplicationController becomes global to all the Controllers within your application. So, we will place the method containing the filter logic in ApplicationController.

To check whether a user is authentic or not, the simplest way is to check whether a session exists for that person or not. If it exists, then we can continue with the normal execution. Let us name it check_authentic_user. The implementation will be as follows:

	def check_authentic_user
		unless session[:user_id]
		flash[:notice] = "Please log in"
		redirect_to(:controller => "user", :action =>
			"index")
		end
	end

It checks for the user_id key in a session. If it is not present, the user is redirected to the login page. Place the code in the application.rb file as a method of ApplicationController. Next, let us use it as a filter. First, we will tell UserController to apply the filter for all the action methods except index and authenticate methods. Add the following statement to the UserController. It should be the first statement after the starting of the Controller class.

	class UserController < ApplicationController
	before_filter :check_authentic_user, :except =>[ :index, :authenticate
	]
	:
	:
	end

Similarly, we will place the filter in other Controllers as well. However, in their case, there are no exceptions. So TaleController will have:

	class TaleController < ApplicationController
	before_filter :check_authentic_user
	:
	:
	end

GenreController and RoleController will be the same as TaleController. Thus, we have completed the ‘applying authorization’ part for the time being. Now, let’s tie up one loose end—the problem of adding a new tale.

Tying Up the Loose Ends

When we developed the User management, the Tale management was affected as the tales table has a many-to-one relationship with the users table. Now we can solve the problem created by the foreign key reference. First, open the user.rb file and add the following statement indicating that it is at the ‘one’ end of the relationship:

	has_many :tale

After addition of the statement, the class will look like the following:

	class User < ActiveRecord::Base
		validates_presence_of :user_name, :password, :first_name,
		:last_name, :age, :email, :country
		validates_uniqueness_of :user_name
		validates_numericality_of :age
		validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-
			z0-9]+\.)+[a-z]{2,})\Z/i
		belongs_to :role
		has_many :tale
		def check_login
			User.login(self.name, self.password)
		end
		def self.login(name,password)
			find(:first,:conditions => ["user_name = ? and password
				=?",name, password])
		end
	end

Next, add the following statement to the tale.rb file:

	belongs_to :user

The file will look like as follows:

	class Tale < ActiveRecord::Base
		validates_presence_of :title, :body_text, :source
		belongs_to:genre
		belongs_to :user
	end

Next, open the tale_controller.rb file. In the create method, we need to add the user’s id to the tale’s user id reference so that the referential integrity can be satisfied. For that, we will get the current user’s id from the session and set it as the value of the user_id attribute of the tale object. The create method will look like as follows, after doing the changes:

	def create
		@tale = Tale.new(params[:tale])
		@tale.genre_id=params[:genre]
		@tale.user_id=session[:user_id]

		@tale.status="new"
		if @tale.save
			flash[:notice] = 'Tale was successfully created.'
			redirect_to :action => 'list'
		else
			render :action => 'new'
		end
	end

That’s it. The ‘loose ends’ related to the User management are tied up. Now let us move onto the Comment Management module.

Developing the Comment Management Module

From the description of functionalities, we know that the module needs to support only three operations—add, view, and delete. The steps for developing the module are almost the same:

  • Generating the Scaffold
  • Modifying the Model
  • Refining the View
  • Customizing the Controller

We have changed the order of refining the view and customizing the Controller steps. That’s what I meant by ‘almost the same’. Let’s get into the development.

Generating the Scaffold

Open the RoR prompt using use_ruby command, and enter the following command:

C:\InstantRails\rails_apps\talewiki>ruby script/generate scaffold Comment comment list show new create destroy

You will get the following screen:

If the scaffold command is reused, then it will not rewrite the existing files unless you specify the -force parameter. We need only new, list, and delete functionalities. So, we have specified the actions that we need—list, show for listing of comments, new and create for adding, and delete for deleting. However, it will still create the stubs and links that need to be tackled at the View level. First, let us do the required modifications at the Model level.

Modifying the Model

First, we have to tell RoR which fields should not be empty. For that, add the validates_presence_of method with :comment_body as the argument in the comment.rb file. After addition, the code shall be as follows:

	class Comment < ActiveRecord::Base
		validates_presence_of :comment_body
	end

Next, we have to tell that the comments table is at the ‘many’ end of the relationship with both tales and users table. For that, add a belongs_to declaration to the comment.rb file.

	class Comment < ActiveRecord::Base
		validates_presence_of :comment_body
		belongs_to :tale
		belongs_to :user
	end

The next step is to tell both the users and the tales table that they are at the ‘one’ end of the relationship. For that, open the user.rb and tale.rb files, and add the has_many declaration. After the additions, the code will be as follows for user.rb:

	class User < ActiveRecord::Base
		validates_presence_of :user_name, :password, :first_name,
		:last_name, :age, :email, :country
		validates_uniqueness_of :user_name
		validates_numericality_of :age
		validates_format_of :email, :with => /\A([^@\s]+)@((?:[-az0-
			9]+\.)+[a-z]{2,})\Z/i
		belongs_to :role
		has_many :tale
		has_many :comment
		def check_login
			User.login(self.name, self.password)
		end

		def self.login(name,password)
			find(:first,:conditions => ["user_name = ? and password
				=?",name, password])
		end
	end

For tale.rb, here is the code:

	class Tale < ActiveRecord::Base
		validates_presence_of :title, :body_text, :source
		belongs_to:genre
		belongs_to :user
		has_many :comment
	end

That completes the changes to be done at the Model level. Next, let us refine the View.

Refining the View

Comments will be given for a story. That means the page displaying a tale will have a link to add comments. This also means that the Comment management module is not a ‘standalone’ module like others, as it will not have its own menu when we decide upon the template. Now coming back to links to the comments in the tale display page, for what functionalities do we need the links? The answer is two—adding a comment and listing the comment. The add comment link will lead to the ‘New Comment’ page, and the view comments link will lead to the list view of the comments. Now let us see what are the problems—each comment needs a user id and the id of the tale for which the comment is being added. The listing of comments needs only the id of the tale. As user id is available from the session, we have to add only the tale id as a part of the link. That is what we are going to do.

Open the show.rhtml file from the app/views/tale directory. It contents are as follows

	<% for column in Tale.content_columns %>
	<p>
		<%= column.human_name %>: <%=h @tale.send(column.name) %>
	</p>
	<% end %>
	<%= link_to 'Edit', :action => 'edit', :id => @tale %>
	<%= link_to 'Back', :action => 'list' %>

Now let us add two more links—one for adding a comment and another for listing the comments:

	<% for column in Tale.content_columns %>
	<p>
		<%= column.human_name %>: <%=h @tale.send(column.name) %>
	</p>
	<% end %>

	<%= link_to 'Edit', :action => 'edit', :id => @tale %>
	<%= link_to 'Back', :action => 'list' %>
	<%= link_to 'Add Comment',:controller=>'comment', :action => 'new', :
	id => @tale.id %>
	<%= link_to 'View Comments',:controller=>'comment', :action => 'list',
	:id => @tale.id %>

The next change we have to do is remove the edit option from the viewing part of the comments. So open the list.rhtml file from the app/views/comments. The code will be as follows:

	<h1>Listing comments</h1>
	<table>
		<tr>
		<% for column in Comment.content_columns %>
			<th><%= column.human_name %></th>
		<% end %>
		</tr>

		<% for comment in @comments %>
		<tr>
		<% for column in Comment.content_columns %>
			<td><%=h comment.send(column.name) %></td>
		<% end %>
			<td><%= link_to 'Show', :action => 'show', :id => comment %></td>
			<td><%= link_to 'Edit', :action => 'edit', :id => comment %></td>
			<td><%= link_to 'Destroy', { :action => 'destroy', :id => comment
				}, :confirm => 'Are you sure?', :method => :post %></td>
		</tr>
	<% end %>
	</table>
	<%= link_to 'Previous page', { :page => @comment_pages.current.
	previous } if @comment_pages.current.previous %>
	<%= link_to 'Next page', { :page => @comment_pages.current.next } if @
	comment_pages.current.next %>
	<br />
	<%= link_to 'New comment', :action => 'new' %>

Delete the tags that link to the Edit and New Comment functionalities. We do not need anyone adding a comment without reading the story. After deletions, the code will be as follows:

	<h1>Listing comments</h1>
	<table>
		<tr>
		<% for column in Comment.content_columns %>
			<th><%= column.human_name %></th>
		<% end %>
		</tr>
		<% for comment in @comments %>
		<tr>
		<% for column in Comment.content_columns %>
			<td><%=h comment.send(column.name) %></td>
		<% end %>
			<td><%= link_to 'Show', :action => 'show', :id => comment %></td>
			<td><%= link_to 'Destroy', { :action => 'destroy', :id => comment
				}, :confirm => 'Are you sure?', :method => :post %></td>
		</tr>
		<% end %>
	</table>
	<%= link_to 'Previous page', { :page => @comment_pages.current.
		previous } if @comment_pages.current.previous %>
	<%= link_to 'Next page', { :page => @comment_pages.current.next } if @
		comment_pages.current.next %>
	<br />

That completes the refinement to be done to the VIEW. Now let’s modify the Controller.

Customizing the Controller

Open the comment_controller.rb file and in the new method add the tale_id to the session object so that the method looks like the following:

	def new
		@comment = Comment.new
		session[:tale_id]=params[:id]
	end

Now in the create method, let us get the tale_id and the user_id from the session, and pass it to the comment object. We have used the session object because the tale_id is coming as a part of the get request, which will be available only to the new method and not the create method. After the changes, the create method will be as follows:

	def create
		@comment = Comment.new(params[:comment])
		@comment.tale_id=session[:tale_id]
		@comment.user_id=session[:user_id]
		if @comment.save
			flash[:notice] = 'Comment was successfully created.'
			redirect_to :action => 'list'
		else
			render :action => 'new'
		end
	end

We do not want to show the list of comments, once a comment has been added. Therefore, we will redirect the user to the tale’s list once a comment has been added successfully.

	def create
		@comment = Comment.new(params[:comment])
		@comment.user_id=session[:user_id]
		@comment.tale_id=session[:tale_id]
		if @comment.save
			flash[:notice] = 'Comment was successfully created.'
			redirect_to :controller=>'tale', :action => 'list'
		else
			render :action => 'new'
		end
	end

Apart from this, we have to change the list method so that it finds that only those
comments are selected for which the tale_id has been passed through the link. So
let us modify the paginate method in the list method to add a condition. After
modification, the list method will be as follows:

	def list
		@comment_pages, @comments = paginate :comments, :
		conditions=>['tale_id = ?',
		params[:id]] :per_page => 10
	end

As you can see, the paginate method takes the table to paginate, the condition which is optional and the number of items to be shown per page as arguments.

And that completes our current work on the Comment management module. Now it is testing time!

Testing the Module

Let us start with the authorization part. Give the following URL at the address bar:


http://localhost:3000/tale

If you get the following screen, it means authorization is working fine:

Next let us test the login functionality. Firstly, give the wrong User name and Password (give anything). If you get the following screen, then the changes are working fine:

Now, give the correct User name/Password combination. I am giving tester as User name and testing as password. If you get the following screen, then authentication is working fine, and also the redirection is doing what it is supposed to do.

Now click on the list link of the first tale and you will get the following screen:

On the detail page, click on the Add Comment link. The following screen will be displayed:

Give the following inputs:

Comment Body—This is a test.

Submission Date—(leave the default date)

Now click on Create. Then, if you get the following screen you can rest assured that everything is working as planned.

Now click on the Show link again and select the View Comments link. If you get the following screen, then the functionality is working:

These tests tell us that the changes we did are working fine. And that completes our ‘endeavour’ on gathering the user comments.

Summary

We have completed Login management and Comment management. Login management was one of the loose ends from the User management part. Now we can concentrate on enhancing the developed modules. These enhancements include custom template creation, the logout option, database-independent table creation, and other features that need to be completed before moving on to developing the new functionalities. These enhancements will implemented in the next chapter. So keep reading!

Comments

comments

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better