Skip to content

Instantly share code, notes, and snippets.

@pascalesdedy
Created October 21, 2018 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pascalesdedy/eab4f74e4909f49719f6f8d2f5d7f681 to your computer and use it in GitHub Desktop.
Save pascalesdedy/eab4f74e4909f49719f6f8d2f5d7f681 to your computer and use it in GitHub Desktop.
app/controller/articles_controller.rb - Simple rails api server TDD
class ArticlesController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_article, only: [:show, :update, :destroy]
# GET /articles
def index
@articles = Article.all
json_response(@articles)
end
# POST /articles
def create
@article = Article.create!(article_params)
json_response(@article,:created)
end
# GET /articles/:id
def show
json_response(@article)
end
# PUT /articles/:id
def update
@article.update(article_params)
head :no_content
end
# DELETE /articles/:id
def destroy
@article.destroy
head :no_content
end
private
def article_params
# whitelist params
params.permit(:title, :content)
end
def set_article
@article = Article.find(params[:id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment