模型:
class Post < ApplicationRecord
has_many_attached :images
end
控制器 update 方法变更后:
def update
respond_to do |format|
if @post.update(post_params.reject { |k| k["images"] })
if post_params[:images].present?
post_params[:images].each do |image|
@post.images.attach(image)
end
end
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end