#7 ぼちぼち
This commit is contained in:
@@ -3,14 +3,28 @@ class PostsController < ApplicationController
|
||||
|
||||
# GET /posts
|
||||
def index
|
||||
@posts = Post.all
|
||||
|
||||
if params[:tags].present?
|
||||
tag_names = params[:tags].split(',')
|
||||
match_type = params[:match]
|
||||
if match_type == 'any'
|
||||
@posts = Post.joins(:tags).where(tags: { name: tag_names }).distinct
|
||||
else
|
||||
@posts = Post.joins(:tags)
|
||||
tag_names.each do |tag|
|
||||
@posts = @posts.where(id: Post.joins(:tags).where(tags: { name: tag }))
|
||||
end
|
||||
@posts = @posts.distinct
|
||||
end
|
||||
else
|
||||
@posts = Post.all
|
||||
end
|
||||
render json: @posts
|
||||
end
|
||||
|
||||
# GET /posts/1
|
||||
def show
|
||||
render json: @post
|
||||
@post = Post.includes(:tags).find(params[:id])
|
||||
render json: @post.as_json(include: { tags: { only: [:id, :name] } })
|
||||
end
|
||||
|
||||
# POST /posts
|
||||
|
||||
Reference in New Issue
Block a user