This commit is contained in:
2025-06-15 22:34:00 +09:00
parent fe8739b290
commit 729bb5e4ca
6 changed files with 114 additions and 30 deletions
+10 -18
View File
@@ -3,8 +3,6 @@ require 'nokogiri'
class PostsController < ApplicationController
before_action :set_post, only: %i[ show update destroy ]
# GET /posts
def index
if params[:tags].present?
@@ -71,27 +69,21 @@ class PostsController < ApplicationController
# PATCH/PUT /posts/1
def update
if @post.update(post_params)
render json: @post
return head :unauthorized unless current_user
return head :forbidden unless ['admin', 'member'].include?(current_user.role)
post = Post.find(params[:id])
tag_ids = JSON.parse(params[:tags])
if post.update(title: params[:title], tags: Tag.where(id: tag_ids))
render({ json: (post
.as_json(include: { tags: { only: [:id, :name, :category] } })),
status: :created })
else
render json: @post.errors, status: :unprocessable_entity
render json: post.errors, status: :unprocessable_entity
end
end
# DELETE /posts/1
def destroy
@post.destroy!
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params.expect(:id))
end
# Only allow a list of trusted parameters through.
def post_params
params.expect(post: [ :title, :body ])
end
end