Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. class ProductsController < ApplicationController
  2. before_action :set_product, only: [:show, :edit, :update, :destroy]
  3. load_and_authorize_resource :except => [:index, :show]
  4. respond_to :json, :html
  5.  
  6. # GET /products
  7. # GET /products.json
  8. def index
  9. if ENV['RAILS_ENV'] == "production"
  10. if params[:q]
  11. search_term = params[:q]
  12. @products = Product.where("name ilike ?", "%#{search_term}%")
  13. else
  14. @products = Product.all
  15. end
  16. else
  17. if params[:q]
  18. search_term = params[:q]
  19. @products = Product.where("name LIKE ?", "%#{search_term}%")
  20. else
  21. @products = Product.all
  22. end
  23. end
  24. respond_with @products
  25. end
  26.  
  27. # GET /products/1
  28. # GET /products/1.json
  29. def show
  30. @comments = @product.comments.order('created_at DESC').page(params[:page]).per_page(2)
  31. end
  32.  
  33. # GET /products/new
  34. def new
  35. @product = Product.new
  36. end
  37.  
  38. # GET /products/1/edit
  39. def edit
  40. end
  41.  
  42. # POST /products
  43. # POST /products.json
  44. def create
  45. @product = Product.new(product_params)
  46.  
  47. respond_to do |format|
  48. if @product.save
  49. format.html { redirect_to @product, notice: 'Product was successfully created.' }
  50. format.json { render :show, status: :created, location: @product }
  51. else
  52. format.html { render :new }
  53. format.json { render json: @product.errors, status: :unprocessable_entity }
  54. end
  55. end
  56. end
  57.  
  58. # PATCH/PUT /products/1
  59. # PATCH/PUT /products/1.json
  60. def update
  61. respond_to do |format|
  62. if @product.update(product_params)
  63. format.html { redirect_to @product, notice: 'Product was successfully updated.' }
  64. format.json { render :show, status: :ok, location: @product }
  65. else
  66. format.html { render :edit }
  67. format.json { render json: @product.errors, status: :unprocessable_entity }
  68. end
  69. end
  70. end
  71.  
  72. # DELETE /products/1
  73. # DELETE /products/1.json
  74. def destroy
  75. @product.destroy
  76. respond_to do |format|
  77. format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
  78. format.json { head :no_content }
  79. end
  80. end
  81.  
  82. private
  83. # Use callbacks to share common setup or constraints between actions.
  84. def set_product
  85. @product = Product.find(params[:id])
  86. end
  87.  
  88. # Never trust parameters from the scary internet, only allow the white list through.
  89. def product_params
  90. params.require(:product).permit(:name, :description, :image_url, :color, :price)
  91. end
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement