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.
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
-> 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