This commit is contained in:
@@ -24,7 +24,12 @@ class ThreadPostsController < ApplicationController
|
||||
def create
|
||||
post = @thread.posts.new(post_params)
|
||||
if post.save
|
||||
render json: post, status: :created
|
||||
render json: post.as_json.merge(image_url: (
|
||||
if post.image.attached?
|
||||
Rails.application.routes.url_helpers.rails_blob_url(post.image, only_path: true)
|
||||
else
|
||||
nil
|
||||
end)), status: :created
|
||||
else
|
||||
render json: { errors: post.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
@@ -37,6 +42,8 @@ class ThreadPostsController < ApplicationController
|
||||
end
|
||||
|
||||
def post_params
|
||||
params.require(:post).permit(:name, :body, :password)
|
||||
params.permit(:name, :message, :password, :image).transform_values { |v|
|
||||
v.presence
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,7 +2,16 @@ class Post < ApplicationRecord
|
||||
belongs_to :thread, class_name: 'Topic', foreign_key: :thread_id
|
||||
has_one_attached :image
|
||||
|
||||
before_create :set_post_no
|
||||
|
||||
has_secure_password validations: false
|
||||
|
||||
scope :active, -> { where deleted_at: nil }
|
||||
|
||||
private
|
||||
|
||||
def set_post_no
|
||||
max_no = Post.where(thread_id:).maximum(:post_no) || 0
|
||||
self.post_no = max_no + 1
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user