-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ubuntu
committed
Mar 7, 2021
1 parent
3224085
commit c1cea7f
Showing
10 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Product < ApplicationRecord | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>Editing Product</h1> | ||
|
||
<%= render 'form', product: @product %> | ||
<%= link_to 'Show', @product %> | | ||
<%= link_to 'Back', products_path %> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1>New Product</h1> | ||
|
||
<%= render 'form', product: @product %> | ||
<%= link_to 'Back', products_path %> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.