Guest User

Untitled

a guest
Apr 11th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.02 KB | None | 0 0
  1. class DocsController < ApplicationController
  2.   before_action :find_doc_by_id, only: %i[show edit update destroy]
  3.  
  4.   def index
  5.     if Doc.exists?(user_id: current_user)
  6.         @docs = Doc.where(user_id: current_user)
  7.     else
  8.         @docs = Doc.all.order('id DESC')
  9.     end
  10.   end
  11.  
  12.   def show; end
  13.  
  14.   def new
  15.     @doc = current_user.docs.build
  16.   end
  17.  
  18.   def create
  19.     @doc = current_user.docs.build(doc_params)
  20.  
  21.     if @doc.save
  22.       redirect_to @doc # does refresh
  23.     else
  24.       render 'new' # doesn;t refresh
  25.     end
  26.   end
  27.  
  28.   def edit; end
  29.  
  30.   def update
  31.     if @doc.update(doc_params)
  32.       redirect_to @doc # does refresh
  33.     else
  34.       render 'edit' # doesn;t refresh
  35.     end
  36.   end
  37.  
  38.   def destroy
  39.     @doc.destroy
  40.     redirect_to docs_path
  41.   end
  42.  
  43.   private
  44.  
  45.   # @param [Object] e
  46.   def find_doc_by_id
  47.     @doc = Doc.find(params[:id])
  48.   rescue StandardError => e
  49.     puts e.inspect
  50.     e.error_message
  51.   end
  52.  
  53.   def doc_params
  54.     params.require(:doc).permit(:title, :content)
  55.   end
  56. end
Advertisement
Add Comment
Please, Sign In to add comment