-
Notifications
You must be signed in to change notification settings - Fork 13
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
1 parent
47839df
commit 2093c17
Showing
12 changed files
with
173 additions
and
7 deletions.
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
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,5 +1,74 @@ | ||
class ProductsController < ApplicationController | ||
before_action :set_product, only: [:show, :edit, :update, :destroy] | ||
|
||
# GET /products | ||
# GET /products.json | ||
def index | ||
@products = Product.all | ||
end | ||
|
||
# GET /products/1 | ||
# GET /products/1.json | ||
def show | ||
end | ||
|
||
# GET /products/new | ||
def new | ||
@product = Product.new | ||
end | ||
|
||
# GET /products/1/edit | ||
def edit | ||
end | ||
|
||
# POST /products | ||
# POST /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 } | ||
format.json { render json: @product.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /products/1 | ||
# PATCH/PUT /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 } | ||
format.json { render json: @product.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /products/1 | ||
# DELETE /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 | ||
|
||
# Never trust parameters from the scary internet, only allow the white list 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,25 @@ | ||
<%= form_for(@product) do |f| %> | ||
<% if @product.errors.any? %> | ||
<div id="error_explanation"> | ||
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2> | ||
|
||
<ul> | ||
<% @product.errors.full_messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div class="field"> | ||
<%= f.label :name %><br> | ||
<%= f.text_field :name %> | ||
</div> | ||
<div class="field"> | ||
<%= f.label :price %><br> | ||
<%= f.text_field :price %> | ||
</div> | ||
<div class="actions"> | ||
<%= f.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' %> | ||
|
||
<%= 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 |
---|---|---|
@@ -1,3 +1,28 @@ | ||
<h1>Products#index</h1> | ||
<h1>Listing products</h1> | ||
|
||
<%= render @products %> | ||
<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 'Add to cart', shopping_cart_path(:product_id => product), :method => 'POST' %></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,4 @@ | ||
json.array!(@products) do |product| | ||
json.extract! product, :id, :name, :price | ||
json.url product_url(product, format: :json) | ||
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,5 @@ | ||
<h1>New product</h1> | ||
|
||
<%= render 'form' %> | ||
|
||
<%= 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,18 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<p> | ||
<strong>Name:</strong> | ||
<%= @product.name %> | ||
</p> | ||
|
||
<p> | ||
<strong>Price:</strong> | ||
<%= @product.price %> | ||
</p> | ||
|
||
<p> | ||
<%= link_to 'Add to cart', shopping_cart_path(:product_id => @product), :method => 'POST' %> | ||
</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 |
---|---|---|
@@ -0,0 +1 @@ | ||
json.extract! @product, :id, :name, :price, :created_at, :updated_at |
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,4 +1,6 @@ | ||
<div> | ||
<div><%= shopping_cart_item.item.name %></div> | ||
<div><%= shopping_cart_item.item.price %></div> | ||
<% if shopping_cart_item.item %> | ||
<div><%= shopping_cart_item.item.name %></div> | ||
<div><%= shopping_cart_item.item.price %></div> | ||
<% end %> | ||
</div> |
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
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,4 +1,6 @@ | ||
Rails.application.routes.draw do | ||
resources :products, only: [ :index ] | ||
resources :products | ||
resource :shopping_cart | ||
|
||
root 'products#index' | ||
end |