e_Marketplace -Rails Project

Lutful Kabir
2 min readMay 9, 2021

The e_Marketplace App was built using Ruby on Rails framework. User can find the product for their daily needs from broader collection of category in this app. An admin can maintain this app by adding or deleting more categories and products. Admin has access to every possible way to control this app.

My app is pretty basic, so I created my own authentication and authorization methods without using gems like Devise. In order to make the registration process easier, I have included a third party log in with a single provider Google. After logged in, user can choose product from the listed category, view their cart, add the product to their cart and remove it. They can update their info and also they can delete their account. If the user is admin, they can add, update and delete a category and product. Can make admin for the app.

Here are the steps I used to create my project application.

1. Create Application With Rails Framework

To create files with Rails on the terminal just type:

$ rails new app_name

2. Add Gem and Install Required Gems

By default Rails applications manage gem dependencies with Bundler. I added the gems below to my gemfile.

gem ‘omniauth-rails_csrf_protection’
gem ‘omniauth-google-oauth2’
gem ‘dotenv-rails’
gem ‘omniauth’
gem ‘bcrypt’
gem ‘faker’
gem ‘pry’

Run bundle:
$ bundle_install

3. Create the Database

Example of a table to create users table.

class CreateUsers < ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.string :username
t.string :email
t.string :password_digest
t.boolean :admin, default: false
t.timestamps
end
end
end

Use $ rails db:create command to run the migration.

4. Create Models

Here is an example of my app/models/user.rb file. I have added validations and to secure password, I included the gem bcrypt in my gemfile .

The has_secure_password adds methods to authenticate against a bcrypt password. This mechanism requires you to have a password_digest attribute. This is created to secure your app if you are not using gems to secure your app.

class User < ApplicationRecord
has_secure_password

validates :username, presence: true, uniqueness: true
validates :email, presence: true, uniqueness: true
end

These validations will ensure that users have a username, email and password and the email and username is unique.

5. Create Views

View directly interact with the user. This is where I made sure my app look nice and how my user interacts with it.

User view:
_errors.html.erb
_form.html.erb
edit.html.erb
index.html.erb
new.html.erb
show.html.erb

6. Create Controller

It’s the engine of the application and ties model and view together.
User controller:

def new
@user = User.new
render :layout => false
end

def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to root_path
else
render :new
end
end

After finish my project and MVC is up and running, started to design the app using inline css and refactor code in ‘dry’ manner. To build everything from scratch was definitely a challenge, but I learned a lot throughout the process.

--

--