Created
October 21, 2018 19:18
-
-
Save pascalesdedy/eab4f74e4909f49719f6f8d2f5d7f681 to your computer and use it in GitHub Desktop.
app/controller/articles_controller.rb - Simple rails api server TDD
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 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