您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

81 行
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 }
  30. format.js { @current_item = @line_item }
  31. format.json { render :show, status: :created, location: @line_item }
  32. else
  33. format.html { render :new }
  34. format.json { render json: @line_item.errors, status: :unprocessable_entity }
  35. end
  36. end
  37. end
  38. # PATCH/PUT /line_items/1
  39. # PATCH/PUT /line_items/1.json
  40. def update
  41. respond_to do |format|
  42. if @line_item.update(line_item_params)
  43. format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
  44. format.json { render :show, status: :ok, location: @line_item }
  45. else
  46. format.html { render :edit }
  47. format.json { render json: @line_item.errors, status: :unprocessable_entity }
  48. end
  49. end
  50. end
  51. # DELETE /line_items/1
  52. # DELETE /line_items/1.json
  53. def destroy
  54. @line_item.destroy
  55. respond_to do |format|
  56. format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
  57. format.json { head :no_content }
  58. end
  59. end
  60. private
  61. # Use callbacks to share common setup or constraints between actions.
  62. def set_line_item
  63. @line_item = LineItem.find(params[:id])
  64. end
  65. # Never trust parameters from the scary internet, only allow the white list through.
  66. def line_item_params
  67. params.require(:line_item).permit(:product_id)
  68. end
  69. end