Skip to content

Commit

Permalink
scaffold products
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Mar 7, 2021
1 parent 3224085 commit c1cea7f
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 1 deletion.
69 changes: 69 additions & 0 deletions app/controllers/products_controller.rb
@@ -0,0 +1,69 @@
class ProductsController < ApplicationController
before_action :set_product, only: %i[ show edit update destroy ]

# GET /products or /products.json
def index
@products = Product.all
end

# GET /products/1 or /products/1.json
def show
end

# GET /products/new
def new
@product = Product.new
end

# GET /products/1/edit
def edit
end

# POST /products or /products.json
def create
@product = Product.new(product_params)

respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: "Product was successfully created." }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /products/1 or /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: "Product was successfully updated." }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end

# DELETE /products/1 or /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: "Product was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end

# Only allow a list of trusted parameters through.
def product_params
params.require(:product).permit(:name, :price)
end
end
2 changes: 2 additions & 0 deletions app/models/product.rb
@@ -0,0 +1,2 @@
class Product < ApplicationRecord
end
27 changes: 27 additions & 0 deletions app/views/products/_form.html.erb
@@ -0,0 +1,27 @@
<%= form_with(model: product) do |form| %>
<% if product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>

<ul>
<% product.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>

<div class="field">
<%= form.label :price %>
<%= form.number_field :price %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/products/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing Product</h1>

<%= render 'form', product: @product %>
<%= link_to 'Show', @product %> |
<%= link_to 'Back', products_path %>
29 changes: 29 additions & 0 deletions app/views/products/index.html.erb
@@ -0,0 +1,29 @@
<p id="notice"><%= notice %></p>

<h1>Products</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= product.price %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Product', new_product_path %>
5 changes: 5 additions & 0 deletions app/views/products/new.html.erb
@@ -0,0 +1,5 @@
<h1>New Product</h1>

<%= render 'form', product: @product %>
<%= link_to 'Back', products_path %>
14 changes: 14 additions & 0 deletions app/views/products/show.html.erb
@@ -0,0 +1,14 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Name:</strong>
<%= @product.name %>
</p>

<p>
<strong>Price:</strong>
<%= @product.price %>
</p>

<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
1 change: 1 addition & 0 deletions config/routes.rb
@@ -1,3 +1,4 @@
Rails.application.routes.draw do
resources :products
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
10 changes: 10 additions & 0 deletions db/migrate/20210307202032_create_products.rb
@@ -0,0 +1,10 @@
class CreateProducts < ActiveRecord::Migration[6.1]
def change
create_table :products do |t|
t.string :name
t.integer :price

t.timestamps
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c1cea7f

Please sign in to comment.