Skip to content

Instantly share code, notes, and snippets.

@raphox
Created July 28, 2023 14:38
Show Gist options
  • Save raphox/e7202e85e2ca6ae14da9b2adabc684d0 to your computer and use it in GitHub Desktop.
Save raphox/e7202e85e2ca6ae14da9b2adabc684d0 to your computer and use it in GitHub Desktop.
documents_controller.rb
class DocumentsController < ApplicationController
before_action :set_document, only: %i[ show update destroy ]
# GET /documents
def index
@documents = Document.all
render json: @documents
end
# GET /documents/1
def show
render json: @document
end
# POST /documents
def create
@document = Document.new(document_params)
if @document.save
render json: @document, status: :created, location: @document
else
render json: @document.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /documents/1
def update
if @document.update(document_params)
render json: @document
else
render json: @document.errors, status: :unprocessable_entity
end
end
# DELETE /documents/1
def destroy
@document.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_document
@document = Document.find(params[:id])
end
# Only allow a list of trusted parameters through.
def document_params
params.require(:document).permit(:title, :description, :link)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment