Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

80 righe
2.1 KiB

  1. class LineItemsController < ApplicationController
  2. include CurrentCart
  3. before_action :set_cart, only: [:create]
  4. before_action :set_line_item, only: [:show, :edit, :update, :destroy]
  5. # TODO: Create counter from play time.
  6. # GET /line_items
  7. # GET /line_items.json
  8. def index
  9. @line_items = LineItem.all
  10. end
  11. # GET /line_items/1
  12. # GET /line_items/1.json
  13. def show
  14. end
  15. # GET /line_items/new
  16. def new
  17. @line_item = LineItem.new
  18. end
  19. # GET /line_items/1/edit
  20. def edit
  21. end
  22. # POST /line_items
  23. # POST /line_items.json
  24. def create
  25. product = Product.find(params[:product_id])
  26. @line_item = @cart.add_product(product)
  27. respond_to do |format|
  28. if @line_item.save
  29. format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
  30. format.json { render :show, status: :created, location: @line_item }
  31. else
  32. format.html { render :new }
  33. format.json { render json: @line_item.errors, status: :unprocessable_entity }
  34. end
  35. end
  36. end
  37. # PATCH/PUT /line_items/1
  38. # PATCH/PUT /line_items/1.json
  39. def update
  40. respond_to do |format|
  41. if @line_item.update(line_item_params)
  42. format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
  43. format.json { render :show, status: :ok, location: @line_item }
  44. else
  45. format.html { render :edit }
  46. format.json { render json: @line_item.errors, status: :unprocessable_entity }
  47. end
  48. end
  49. end
  50. # DELETE /line_items/1
  51. # DELETE /line_items/1.json
  52. def destroy
  53. @line_item.destroy
  54. respond_to do |format|
  55. format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
  56. format.json { head :no_content }
  57. end
  58. end
  59. private
  60. # Use callbacks to share common setup or constraints between actions.
  61. def set_line_item
  62. @line_item = LineItem.find(params[:id])
  63. end
  64. # Never trust parameters from the scary internet, only allow the white list through.
  65. def line_item_params
  66. params.require(:line_item).permit(:product_id, :cart_id)
  67. end
  68. end