Created
July 28, 2023 14:38
-
-
Save raphox/e7202e85e2ca6ae14da9b2adabc684d0 to your computer and use it in GitHub Desktop.
documents_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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