Advertisement
jarocki_art

testimonial controller

Jul 6th, 2019
2,618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 2.11 KB | None | 0 0
  1. class Admin::TestimonialsController < ApplicationController
  2.   before_action :authenticate_user!
  3.  
  4.   def index
  5.     testimonials = Testimonial.all.select :id, :name, :patronymic, :surname, :userpic, :company,
  6.                                           :object_photo_1, :object_photo_2, :object_photo_3,
  7.                                           :video, :text, :created_at, :updated_at
  8.     render json: testimonials, status: :ok
  9.   end
  10.  
  11.   def create
  12.     testimonial = Testimonial.new(t_params)
  13.  
  14.     if testimonial.save
  15.       testimonial = find_testimonial(testimonial.id)
  16.       render json: testimonial, status: :created
  17.     else
  18.       render json: {errors: testimonial.errors}, status: :bad_request
  19.     end
  20.   end
  21.  
  22.   def show
  23.     testimonial = find_testimonial(params[:id])
  24.     render json: testimonial, status: :ok
  25.   end
  26.  
  27.   def update
  28.     testimonial = Testimonial.find(params[:id])
  29.  
  30.     if testimonial.update_attributes(t_params)
  31.       testimonial = find_testimonial(params[:id])
  32.       render json: testimonial, status: :ok
  33.     else
  34.       render json: {errors: testimonial.errors}, status: :bad_request
  35.     end
  36.   end
  37.  
  38.   def destroy
  39.     testimonial = Testimonial.find(params[:id])
  40.     testimonial.destroy
  41.     render json: testimonial, status: :no_content
  42.   end
  43.  
  44. private
  45.  
  46.   def t_params
  47.     safe_params = params.permit :name, :patronymic, :surname, :userpic, :company,
  48.                                 :object_photo_1, :object_photo_2, :object_photo_3,
  49.                                 :video, :text, :id
  50.  
  51.     params = ActionController::Parameters.new(testimonial: safe_params)
  52.  
  53.     params.require(:testimonial).permit :name, :patronymic, :surname, :userpic, :company,
  54.                                         :object_photo_1, :object_photo_2, :object_photo_3,
  55.                                         :video, :text, :id
  56.   end
  57.  
  58.   def find_testimonial id
  59.     Testimonial.select(:id, :name, :patronymic, :surname, :userpic, :company,
  60.                        :object_photo_1, :object_photo_2, :object_photo_3,
  61.                        :video, :text, :created_at, :updated_at).where(id: id)
  62.   end
  63. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement