mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 07:16:40 +00:00
add formData support
This commit is contained in:
@@ -4,10 +4,19 @@ class BlogsController < ApplicationController
|
||||
|
||||
# POST /blogs
|
||||
def create
|
||||
@blog = Blog.create(params.require(:blog).permit(:title, :content))
|
||||
thumbnail = save_uploaded_file(params[:blog][:thumbnail])
|
||||
@blog = Blog.create(params.require(:blog).permit(:title, :content).merge(:thumbnail => thumbnail))
|
||||
respond_with @blog
|
||||
end
|
||||
|
||||
# Put /blogs/1
|
||||
def upload
|
||||
@blog = Blog.find_by_id(params[:id])
|
||||
return head :not_found if @blog.nil?
|
||||
@blog.thumbnail = save_uploaded_file params[:file]
|
||||
head @blog.save ? :ok : :unprocsessible_entity
|
||||
end
|
||||
|
||||
# GET /blogs
|
||||
def index
|
||||
@blogs = Blog.all
|
||||
@@ -24,4 +33,14 @@ class BlogsController < ApplicationController
|
||||
respond_with @blog, status: :not_found and return unless @blog
|
||||
respond_with @blog
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def save_uploaded_file(field)
|
||||
return if field.nil?
|
||||
require 'fileutils'
|
||||
file = File.join('tmp', field.original_filename)
|
||||
FileUtils.cp field.tempfile.path, file
|
||||
field.original_filename
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user