From 2093c17c67e1f757785607c7e73cabef2afab14a Mon Sep 17 00:00:00 2001 From: chischaschos Date: Tue, 9 Jun 2015 22:23:57 -0500 Subject: [PATCH] Adding code from https://github.com/crowdint/acts_as_shopping_cart_app/pull/2 --- README.md | 12 +++- app/controllers/products_controller.rb | 69 +++++++++++++++++++ app/views/products/_form.html.erb | 25 +++++++ app/views/products/edit.html.erb | 6 ++ app/views/products/index.html.erb | 29 +++++++- app/views/products/index.json.jbuilder | 4 ++ app/views/products/new.html.erb | 5 ++ app/views/products/show.html.erb | 18 +++++ app/views/products/show.json.jbuilder | 1 + .../_shopping_cart_item.html.erb | 6 +- app/views/shopping_carts/show.html.erb | 1 + config/routes.rb | 4 +- 12 files changed, 173 insertions(+), 7 deletions(-) create mode 100644 app/views/products/_form.html.erb create mode 100644 app/views/products/edit.html.erb create mode 100644 app/views/products/index.json.jbuilder create mode 100644 app/views/products/new.html.erb create mode 100644 app/views/products/show.html.erb create mode 100644 app/views/products/show.json.jbuilder diff --git a/README.md b/README.md index 2e15039..0381c1b 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,14 @@ [![Build Status](https://travis-ci.org/crowdint/acts_as_shopping_cart_app.svg?branch=master)](https://travis-ci.org/crowdint/acts_as_shopping_cart_app) -## TODO -- Explain how to use it or where to see it working +## Installation + +```bash +git clone https://github.com/crowdint/acts_as_shopping_cart_app +cd acts_as_shopping_cart_app +bundle exec +bundle install +bundle exec rake db:setup +bundle exec rails s +``` diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index b93cd40..f9893df 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -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 diff --git a/app/views/products/_form.html.erb b/app/views/products/_form.html.erb new file mode 100644 index 0000000..109592b --- /dev/null +++ b/app/views/products/_form.html.erb @@ -0,0 +1,25 @@ +<%= form_for(@product) do |f| %> + <% if @product.errors.any? %> +
+

<%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.label :price %>
+ <%= f.text_field :price %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/products/edit.html.erb b/app/views/products/edit.html.erb new file mode 100644 index 0000000..39f7f4f --- /dev/null +++ b/app/views/products/edit.html.erb @@ -0,0 +1,6 @@ +

Editing product

+ +<%= render 'form' %> + +<%= link_to 'Show', @product %> | +<%= link_to 'Back', products_path %> diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index 4261759..b1ae799 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -1,3 +1,28 @@ -

Products#index

+

Listing products

-<%= render @products %> + + + + + + + + + + + <% @products.each do |product| %> + + + + + + + + + <% end %> + +
NamePrice
<%= product.name %><%= product.price %><%= link_to 'Add to cart', shopping_cart_path(:product_id => product), :method => 'POST' %><%= link_to 'Show', product %><%= link_to 'Edit', edit_product_path(product) %><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Product', new_product_path %> diff --git a/app/views/products/index.json.jbuilder b/app/views/products/index.json.jbuilder new file mode 100644 index 0000000..c4887c6 --- /dev/null +++ b/app/views/products/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@products) do |product| + json.extract! product, :id, :name, :price + json.url product_url(product, format: :json) +end diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb new file mode 100644 index 0000000..e5e0037 --- /dev/null +++ b/app/views/products/new.html.erb @@ -0,0 +1,5 @@ +

New product

+ +<%= render 'form' %> + +<%= link_to 'Back', products_path %> diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb new file mode 100644 index 0000000..4f58ae2 --- /dev/null +++ b/app/views/products/show.html.erb @@ -0,0 +1,18 @@ +

<%= notice %>

+ +

+ Name: + <%= @product.name %> +

+ +

+ Price: + <%= @product.price %> +

+ +

+ <%= link_to 'Add to cart', shopping_cart_path(:product_id => @product), :method => 'POST' %> +

+ +<%= link_to 'Edit', edit_product_path(@product) %> | +<%= link_to 'Back', products_path %> diff --git a/app/views/products/show.json.jbuilder b/app/views/products/show.json.jbuilder new file mode 100644 index 0000000..e25ce28 --- /dev/null +++ b/app/views/products/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @product, :id, :name, :price, :created_at, :updated_at diff --git a/app/views/shopping_carts/_shopping_cart_item.html.erb b/app/views/shopping_carts/_shopping_cart_item.html.erb index 1375bfa..d9c6423 100644 --- a/app/views/shopping_carts/_shopping_cart_item.html.erb +++ b/app/views/shopping_carts/_shopping_cart_item.html.erb @@ -1,4 +1,6 @@
-
<%= shopping_cart_item.item.name %>
-
<%= shopping_cart_item.item.price %>
+ <% if shopping_cart_item.item %> +
<%= shopping_cart_item.item.name %>
+
<%= shopping_cart_item.item.price %>
+ <% end %>
diff --git a/app/views/shopping_carts/show.html.erb b/app/views/shopping_carts/show.html.erb index 04d08d9..f164b42 100644 --- a/app/views/shopping_carts/show.html.erb +++ b/app/views/shopping_carts/show.html.erb @@ -6,3 +6,4 @@
SubTotal:<%= number_to_currency @shopping_cart.subtotal %>
Taxes:<%= number_to_currency @shopping_cart.taxes %>
Total:<%= number_to_currency @shopping_cart.total %>
+ diff --git a/config/routes.rb b/config/routes.rb index 0a3a3de..0fc0cc0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do - resources :products, only: [ :index ] + resources :products resource :shopping_cart + + root 'products#index' end