選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

89 行
2.2 KiB

  1. class ProductsController < ApplicationController
  2. before_action :set_product, only: [:show, :edit, :update, :destroy]
  3. # GET /products
  4. # GET /products.json
  5. def index
  6. @products = Product.all
  7. end
  8. # GET /products/1
  9. # GET /products/1.json
  10. def show
  11. end
  12. # GET /products/new
  13. def new
  14. @product = Product.new
  15. end
  16. # GET /products/1/edit
  17. def edit
  18. end
  19. # POST /products
  20. # POST /products.json
  21. def create
  22. @product = Product.new(product_params)
  23. respond_to do |format|
  24. if @product.save
  25. format.html { redirect_to @product, notice: 'Product was successfully created.' }
  26. format.json { render :show, status: :created, location: @product }
  27. else
  28. format.html { render :new }
  29. format.json { render json: @product.errors, status: :unprocessable_entity }
  30. end
  31. end
  32. end
  33. # PATCH/PUT /products/1
  34. # PATCH/PUT /products/1.json
  35. def update
  36. respond_to do |format|
  37. if @product.update(product_params)
  38. format.html { redirect_to @product, notice: 'Product was successfully updated.' }
  39. format.json { render :show, status: :ok, location: @product }
  40. @products = Product.all
  41. ActionCable.server.broadcast 'products',
  42. html: render_to_string('store/index', layout: false)
  43. else
  44. format.html { render :edit }
  45. format.json { render json: @product.errors, status: :unprocessable_entity }
  46. end
  47. end
  48. end
  49. # DELETE /products/1
  50. # DELETE /products/1.json
  51. def destroy
  52. @product.destroy
  53. respond_to do |format|
  54. format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
  55. format.json { head :no_content }
  56. end
  57. end
  58. def who_bought
  59. @product = Product.find(params[:id])
  60. @latest_order = @product.orders.order(:updated_at).last
  61. if stale?(@latest_order)
  62. respond_to do |format|
  63. format.atom
  64. end
  65. end
  66. end
  67. private
  68. # Use callbacks to share common setup or constraints between actions.
  69. def set_product
  70. @product = Product.find(params[:id])
  71. end
  72. # Never trust parameters from the scary internet, only allow the white list through.
  73. def product_params
  74. params.require(:product).permit(:title, :description, :image_url, :price)
  75. end
  76. end