Show Menu

Table with count method react Cheat Sheet by

react

Step

1. new Rails applic­ation
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/mo­del­s/d­og.rb db/mig­rat­e/[­dat­e_t­ime­]_c­rea­te_­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­/ro­utes.rb file also gets automa­tically updated:

5. Update the config­/ro­utes.rb

Rails.application.routes.draw do
  resources :dogs
end

6. migrate our data

class CreateDogs < ActiveRecord::Migration[5.2]
  def change
    create_table :dogs do |t|
      t.string :name
      t.string :motto
      
      t.timestamps
    end
  end
end
rake db:migrate

7. Update the db/mig­rat­e/s­eeds.rb

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')
rake db:seed

(1).­con­fig­/ro­ute­s.rb

1.config/routes.rb
Rails.application.routes.draw do
  resources :barangs
end

(2)d­b/m­igr­ate­/20­190­510­051­736­_cr­eat­e_b­ara­ngs.rb

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/­see­ds.rb

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)
 

8. added to your database

In order to check if the dogs have been added to your database, run:

rails console or rails c

Type in Dog.all and you should see that the 4 dogs have been added.

FYI. Run rails routes or go to localh­ost­:30­00/­rai­ls/­inf­o/r­outes to see all routes your applic­ation is configured to:

9. Read

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>

10. Read(Show details of specific dog)

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­/co­ntr­oll­ers­/ba­ran­gs_­con­tro­lle­r.rb

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)a­pp/­vie­ws/­bar­ang­s/i­nde­x.h­tml.erb

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).a­pp/­vie­ws/­bar­ang­s/e­dit.ht­ml.e­rb*

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

11. Create­(Create a new dog)

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

12.Update

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

13. Delete­(Remove a dog from the database)

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­/vi­ews­/ba­ran­gs/­new.ht­ml.e­rb

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­/vi­ews­/ba­ran­gs/­sho­w.h­tml.erb

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­/ma­th_­cal­cul­ato­r.rb

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

Download the Table with count method react Cheat Sheet

4 Pages
//media.cheatography.com/storage/thumb/amihapsari_table-with-count-method-react.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          React Reusability Cheat Sheet
          React Cheat Sheet

          More Cheat Sheets by amihapsari

          Table Hitung Angular 6 Cheat Sheet