45 lines
897 B
Ruby
45 lines
897 B
Ruby
class ArticlesController < ApplicationController
|
|
before_action :set_article, only: [:show, :update, :destroy]
|
|
skip_before_action :authorize_request, only: [:index, :show]
|
|
|
|
# GET /articles
|
|
def index
|
|
@article = Article.order(:created_at).reverse.to_json(include: :user)
|
|
json_response(@article)
|
|
end
|
|
|
|
# POST /articles
|
|
def create
|
|
@article = current_user.articles.create!(article_params)
|
|
json_response(@article, :created)
|
|
end
|
|
|
|
# GET /articles/:id
|
|
def show
|
|
json_response(@article.to_json(include: :user))
|
|
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, :post)
|
|
end
|
|
|
|
def set_article
|
|
@article = Article.find(params[:id])
|
|
end
|
|
|
|
end
|