123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- #---
- # Excerpted from "Agile Web Development with Rails",
- # published by The Pragmatic Bookshelf.
- # Copyrights apply to this code. It may not be used to create training material,
- # courses, books, articles, and the like. Contact us if you are in doubt.
- # We make no guarantees that this code is fit for any purpose.
- # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
- #---
- class LineItemsController < ApplicationController
- include CurrentCart
- before_action :set_cart, only: [:create]
- before_action :set_line_item, only: [:show, :edit, :update, :destroy]
- # GET /line_items
- # GET /line_items.json
- def index
- @line_items = LineItem.all
- end
- # GET /line_items/1
- # GET /line_items/1.json
- def show
- end
- # GET /line_items/new
- def new
- @line_item = LineItem.new
- end
- # GET /line_items/1/edit
- def edit
- end
- # POST /line_items
- # POST /line_items.json
- def create
- product = Product.find(params[:product_id])
- @line_item = @cart.add_product(product.id)
- respond_to do |format|
- if @line_item.save
- format.html { redirect_to store_url }
- format.js { @current_item = @line_item }
- format.json { render action: 'show',
- status: :created, location: @line_item }
- else
- format.html { render action: 'new' }
- format.json { render json: @line_item.errors,
- status: :unprocessable_entity }
- end
- end
- end
- # PATCH/PUT /line_items/1
- # PATCH/PUT /line_items/1.json
- def update
- respond_to do |format|
- if @line_item.update(line_item_params)
- format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
- format.json { head :no_content }
- else
- format.html { render action: 'edit' }
- format.json { render json: @line_item.errors, status: :unprocessable_entity }
- end
- end
- end
- # DELETE /line_items/1
- # DELETE /line_items/1.json
- def destroy
- @line_item.destroy
- respond_to do |format|
- format.html { redirect_to line_items_url }
- format.json { head :no_content }
- end
- end
- private
- # Use callbacks to share common setup or constraints between actions.
- def set_line_item
- @line_item = LineItem.find(params[:id])
- end
- # Never trust parameters from the scary internet, only allow the white
- # list through.
- def line_item_params
- params.require(:line_item).permit(:product_id)
- end
- #...
- end
|