|
1. new Rails application
|
rails new dog-app -T
|
|
2. directory to access
|
cd dog-app/
|
|
3. Generate the model
|
rails g model dog name motto
|
|
This creates 2 files: app/models/dog.rb db/migrate/[date_time]_create_dogs.rb The dog table in our database will have 2 fields: name and motto.
|
|
|
4. Generate the controller
|
rails g controller dogs index show new edit
|
|
5. The config/routes.rb file also gets automatically updated:
|
|
Rails.application.routes.draw do
resources :dogs
end
class CreateDogs < ActiveRecord::Migration[5.2]
def change
create_table :dogs do |t|
t.string :name
t.string :motto
t.timestamps
end
end
end
Dog.create(name: 'Nala', motto: 'Born to be wild')
Dog.create(name: 'Alex', motto: 'Calm as can be')
Dog.create(name: 'Leroy', motto: 'Life of the pawty')
Dog.create(name: 'Belle', motto: 'Miss Independent')
1.**config/routes.rb**
Rails.application.routes.draw do
resources :barangs
end
2.**db/migrate/20190510051736_create_barangs.rb**
class CreateBarangs < ActiveRecord::Migration[5.2]
def change
create_table :barangs do |t|
t.string :sku
t.string :nama
t.integer :kuantitas
t.integer :hargaBeli
t.integer :hargaJual
t.integer :profit
t.timestamps
end
end
end
3.**db/seeds.rb**
Barang.create(sku:'AIR001', nama: 'Air mineral Aqui', kuantitas:10, hargaBeli: 3000, hargaJual: 6000, profit: 3000)
Barang.create(sku:'SOF001', nama: 'Choca Chola', kuantitas:20, hargaBeli: 5000, hargaJual: 4500, profit: -500)
Barang.create(sku:'SNK001', nama: 'Cheetah Toh', kuantitas:15, hargaBeli: 4000, hargaJual: 5000, profit:1000)
**Goal: List all dogs**
**Controller** — app/controllers/dogs_controller.rb, index method
def index
@dogs = Dog.all
end
**Index** — views/dogs/index.html.erb
<ul>
<% @dogs.each do |dog| %>
<li><%= link_to dog.name, dog_path(dog) %></li>
<% end %>
</ul>
**Goal**: Show details of specific dog
**Controller **— app/controllers/dogs_controller.rb, show method
def show
@dog = Dog.find(params[:id])
end
**Show** — views/dogs/show.html.erb
<h1><%= @dog.name %></h1>
<h4>"<%= @dog.motto %>"</h4>
4.**app/controllers/barangs_controller.rb**
class BarangsController < ApplicationController
def index
@barangs = Barang.all
end
def show
@barangs = Barang.find(params[:id])
end
def new
@barangs = Barang.new
@hasilProfit = MathCalculator.send(params[:operation], *[params[:a], params[;b]])
render : index
end
def create
@barang = Barang.create(barang_params)
redirect_to barangs_path
end
def barang_params
params.require(:barang).permit(:sku, :nama, :kuantitas, :hargaBeli, :hargaJual, :profit)
end
def edit
@barang = Barang.find(params[:id])
end
def destroy
@barang = Barang.find(params[:id])
@barang.destroy
redirect_to barangs_path
end
def update
@barang = Barang.find(params[:id])
@barang.update(barang_params)
redirect_to barangs_path
end
def get_profit
count_profit = ProfitCalculator.new params[:operation]
@result = profit_calculator.calculate
end
end
5.**app/views/barangs/index.html.erb**
<style>
table{
margin-left: auto;
margin-right: auto;
}
.tabel2 {
border-collapse: collapse;
}
.tabel2, tr, th, td {
border: 1px solid black;
}
</style>
<h1>Daftar Barang</h1>
<table class='tabel2'>
<thead>
<tr>
<th>SKU</th>
<th>Nama</th>
<th>Kuantitas </th>
<th>Harga beli</th>
<th>Harga jual</th>
<th>Profit per item</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
<% @barangs.each do |barang| %>
<tr>
<td scope="row"><%= barang.sku %></td>
<td><%= barang.nama%></td>
<td><%= barang.kuantitas %></td>
<td><%= barang.hargaBeli %></td>
<td><%= barang.hargaJual %></td>%
<td>
<% unless @hasilProfit.nil? %>
<%= @hasilProfit %>
<% end %>
</td>
<td><%= button_to 'Edit', edit_barang_path(barang), method: 'get'%></td>
<td><%= button_to 'Remove', barang_path(barang), method: 'delete', data: { confirm: 'Anda Yakin?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to "Add a New Barang", new_barang_path %>
6.*app/views/barangs/edit.html.erb**
<h1>Edit Barang</h1>
<h3>Update Barang Details</h3>
<%= form_with model: @barang do |form| %>
<%= form.text_field :sku, placeholder: "sku" %>
<%= form.text_field :nama, placeholder: "nama" %>
<%= form.number_field :kuantitas, placeholder: "kuantitas" %>
<%= form.number_field :hargaBeli, placeholder: "hargaBeli" %>
<%= form.number_field :hargaJual, placeholder: "hargaJual" %>
<%= form.number_field :profit, placeholder: "profit" %>
<%= form.submit %>
<% end %>
**Goal**: Create a new dog
**Controller** — app/controllers/dogs_controller.rb, new and create methods
def new
@dog = Dog.new
end
def create
dog = Dog.create(dog_params)
redirect_to dogs_path
end
private
def dog_params
params.require(:dog).permit(:name, :motto)
end
**New** — views/dogs/new.html.erb
<h3>Create a Dog</h3>
<%= form_with model: @dog do |form| %>
<%= form.text_field :name, placeholder: "name" %>
<%= form.text_field :motto, placeholder: "motto" %>
<%= form.submit %>
<% end %>
**Goal** Update details for specific dog
**Controller** — app/controllers/dogs_controller.rb, edit and update methods
def edit
@dog = Dog.find(params[:id])
end
def update
@dog = Dog.find(params[:id])
@dog.update(dog_params)
redirect_to dog_path(@dog)
end
private
def dog_params
params.require(:dog).permit(:name, :motto)
end
**Edit** — views/dogs/edit.html.erb
<h3>Update Dog Details</h3>
<%= form_with model: @dog do |form| %>
<%= form.text_field :name, placeholder: "name" %>
<%= form.text_field :motto, placeholder: "motto" %>
<%= form.submit %>
<% end %>
**Goal**: Remove a dog from the database
**Controller **— app/controllers/dogs_controller.rb, destroy method
def destroy
@dog = Dog.find(params[:id])
@dog.destroy
redirect_to dogs_path
end
**Show** — views/dogs/show.html.erb
<%= link_to 'Remove', @dog, method: :delete, data: { confirm: 'Are you sure?' } %>
7.**app/views/barangs/new.html.erb**
<h1>Buat Data Barang</h1>
<h3>Create Barang</h3>
<%= form_with model: @barangs do |form| %>
<%= form.text_field :sku, placeholder: "sku" %>
<%= form.text_field :nama, placeholder: "nama" %>
<%= form.number_field :kuantitas, placeholder: "kuantitas" %>
<%= form.number_field :hargaBeli, placeholder: "hargaBeli" %>
<%= form.number_field :hargaJual, placeholder: "hargaJual" %>
<%= form.number_field :profit, placeholder: "profit" %>
<%= form.submit %>
<% end %>
8.**app/views/barangs/show.html.erb**
<h1>Lihat Barang</h1>
<h1><%= @barangs.sku %></h1>
<h4><%= @barangs.nama %></h4>
<h4><%= @barangs.kuantitas %></h4>
<h4><%= @barangs.hargaBeli %></h4>
<h4><%= @barangs.hargaJual %></h4>
<h4><%= @barangs.profit %></h4>
9.**lib/math_calculator.rb**
class MathCalculator
def calculateProfit(a, b)
# whatever you need to do here
a.to_i - b.to_i
end
end
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.