Tutorial – Building a Support Ticket System on Rails – Part 2

We are going to create the following in this tutorial:

  • Ticket Model
  • Ticket Controller
  • Ticket Views

Here we going to take the easy way out by having rails generate all the above for us, using a single command.

1. Open Terminal and key the following:

-> rails g scaffold Ticket ticketnumber:integer ticketTitle:string ticketDescription:string status:string date:datetime uid:string resolution:string

Now in the project directory, a lot of new stuff files are generated for you as well as configuration of routes are done up.

2. Now to create the table in the database

-> rake db:migrate

3. Now we need to set the default page that is going to be loaded by the application.

Browse to config/routes.rb, add the following line into the file.

-> root ‘tickets#index’

4. Next we want the application to validate that the user is signed before access any pages.

Browser to app/controllers/tickets_controller.rb, add the following line after the class TicketsController <ApplicationController.

-> before_action :authenticate_user!

5. Testing time!

-> rails s

Browse to http://localhost:3000/


Tutorial – Building a Support Ticket System on Rails – Part 1

From the start of this year, I will try to start writing more tutorial post instead of snippet of technology post.

For a start, I will be talking about the recent Support Ticket System that I build on Rails.

Duration: 30 mins

Let’s create a new Rails application.

1. Open up Terminal, enter the following command to generate the project

-> rails new SupportTicketSystem

This should generate all the relevant files and the necessary files structure.

2. Configuration of Devise

2.1 Access the project folder and locate the Gemfile, add the following line.

gem ‘devise’

We are going to use Devise here to help you manage your users. For more information on Devise visit the following,https://github.com/plataformatec/devise

2.2 Next up from the Terminal, access the folder and run the following command.

-> bundle install

In some case, due to how your machine is setup, you may need to run it will ‘rake’ infront

-> rake bundle install

2.3 Run the following command to setup Devise.

-> rails g devise:install


-> rails generate devise:install

Both of the above does the same thing but the first option is a short hand method of rails.

2.4 Now we are going to specify the Users data so that Devise is aware of it and create the database table.

-> rails g devise User

->  rake db:migrate

To verify the above setup, go to app/models/user.rb

You should see the User.rb being created and that the context of the file should be similar to what I have.

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

2.5 We going to add a new column to the User Model, call Type.

This type will allow the system to know if the user is a customer or a support engineer.

Now go to the terminal again.

-> rails g migration add_type_to_user type:string

-> rake db:migrate

Now you have added a new column to the database table!

2.6 Setting different user type via Enum. Open up User.rb in app/model

Add the following code:

enum type: [:SupportEngineer, :Customer, :Admin]

2.7 Lets generate some UI from Devise

-> rails g devise:views users

2.8 Controller!

-> rails g controller Users

Now go into the file, users_controller.rb in the following location, app/controller

We need to change the “ApplicationController” to Devise::SessionController

class UsersController < Devise::SessionsController

Now go to routes.rb in your app/config and make sure that devise_for:users is there.

2.9 Now to test 

In terminal, do the following command to start the server.

-> rails s

Open up your browser, http://localhost:3000/users/sign_up

WildCard Search in Ruby on Rails

Wildcard Search

Assuming that you have you have the following model:

Model : Book


  • BookName
  • Author
  • PublishedDate
  • Edition

Typical, when you try to do a wildcard search you will do the following code below:

Book.where(“BookName LIKE ‘%#{BookNameInput}%’)

The above returns the following in terms of SQL ->

SELECT “Books”.* FROM “Books” WHERE (BookName LIKE ‘%BookNameInput%’)


This makes you vulnerable to SQL Injection, for more information visit, http://railscasts.com/episodes/25-sql-injection?view=asciicast


Use the following syntax instead:

Book.where(“BookName LIKE (?)” , “%#{BookNameInput}%”)

Additional information:

If you want to do WILDCARD SEARCH and ignoring case sensitivity, use iLIKE instead of LIKE.