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? %> +
Name | +Price | ++ | |||
---|---|---|---|---|---|
<%= 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?' } %> | +
<%= 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 @@