Created
February 22, 2017 01:07
-
-
Save ajosephau/f2c4542e4e57e13f4b23b47d2975dca3 to your computer and use it in GitHub Desktop.
Ruby on rails tutorial - creating a basic accounting/Internet banking system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################ | |
## Create new Rails application | |
################ | |
rails new cfa_accounts | |
cd cfa_accounts | |
################ | |
## Add following lines to Gemfile (skip the record_tag_helper for rails 4) | |
################ | |
gem 'bootstrap-generators' | |
gem 'bootstrap_forms' | |
gem 'record_tag_helper' | |
gem 'devise' | |
################ | |
## Initialise new gems | |
################ | |
#install new gems | |
bundle install | |
# deploy Bootstrap forms to app | |
rails generate bootstrap:install | |
# when prompted, overwrite application.html.erb to templates get Bootstrap forms | |
# restart Rails preloader "spring" to get new Bootstrap libraries | |
spring stop | |
################ | |
## Install devise - user authentication | |
################ | |
rails generate devise:install | |
# follow instructions in prompt | |
# add following line to config/environments/development.rb | |
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } | |
# define a root rul to something in config/routes.rb | |
root to: "accounts#index" | |
# add to app/views/layouts/application.html.erb | |
<p class="notice"><%= notice %></p> | |
<p class="alert"><%= alert %></p> | |
# run to create Devise views | |
rails g devise:views | |
################ | |
## create a User model | |
################ | |
rails generate devise User | |
# add following lines to /app/controllers/application_controller.rb to force logins after protect_from_forgery | |
before_action :authenticate_user! | |
################ | |
## create other models | |
################ | |
rails generate scaffold Account name:string user:references | |
rails generate scaffold Transaction description:string time:datetime account:references | |
rake db:migrate | |
################ | |
## Add has many/belongs to to /app/models files | |
################ | |
class User < ApplicationRecord | |
has_many :accounts | |
end | |
class Account < ApplicationRecord | |
belongs_to :user | |
has_many :transactions | |
end | |
class Transaction < ApplicationRecord | |
belongs_to :account | |
end | |
################ | |
## Customise templates to suit new types in /app/views/layouts/application.html.erb | |
################ | |
<title>Anthony's Accounts</title> | |
<%= link_to "Anthony's Accounts", "/", class: "navbar-brand" %> | |
<li><%= link_to "Accounts", "/accounts" %></li> | |
<li><%= link_to "Transactions", "/transactions" %></li> | |
################ | |
## Add following lines to /config/routes.rb | |
################ | |
root 'accounts#index' | |
################ | |
## Replace form control for selecting users in accounts (/app/views/accounts/_form.html.erb) | |
################ | |
<%= f.collection_select(:user_id, User.all, :id, :email ) %> | |
################ | |
## Replace reference to user's name (/app/views/accounts/show.html.erb and /app/views/accounts/index.html.erb ) | |
################ | |
<dd><%= @account.user.email %></dd> | |
################ | |
## Display name of accounts in list of transactions | |
################ | |
## add ".name" to /app/views/transactions/show.html.erb and index.html.erb | |
@transaction.account.name | |
################ | |
## add reference select to /app/views/transactions/_form.html.erb | |
################ | |
<%= f.collection_select(:account_id, Account.all, :id, :name ) %> | |
################ | |
## Add new field "amount" to model | |
################ | |
# generate migration to add amount to object (note precision = 8, scale = 2 | |
rails generate migration AddAmountToTransactions 'amount:decimal{8,2}' | |
# add field to _form.html.erb | |
<div class="form-group"> | |
<%= f.label :amount, class: "col-sm-2 control-label" %> | |
<div class="col-sm-10"> | |
<%= f.text_field :amount, class: "form-control" %> | |
</div> | |
</div> | |
# add new entry to transactions_controller.rb so form will allow users to enter an amount | |
def transaction_params | |
params.require(:transaction).permit(:description, :time, :amount, :account_id) | |
end | |
# add displaying amount to show.html.erb | |
<dt>Amount:</dt> | |
<dd><%= @transaction.amount %></dd> | |
################ | |
## add summary view of transactions | |
################ | |
# add to model functions | |
def balance | |
self.transactions.sum(:amount) | |
end | |
#Add to index.html.erb and show.html.erb | |
<td><%= account.balance %></td> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment