Created
October 22, 2015 15:02
-
-
Save oojikoo-gist/ac2b267eb36f92a45537 to your computer and use it in GitHub Desktop.
rails: carrierwave_upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# POST /pictures | |
# POST /pictures.json | |
def create | |
#check if file is within picture_path | |
if params[:picture][:picture_path]["file"] | |
picture_path_params = params[:picture][:picture_path] | |
#create a new tempfile named fileupload | |
tempfile = Tempfile.new("fileupload") | |
tempfile.binmode | |
#get the file and decode it with base64 then write it to the tempfile | |
tempfile.write(Base64.decode64(picture_path_params["file"])) | |
#create a new uploaded file | |
uploaded_file = ActionDispatch::Http::UploadedFile.new(:tempfile => tempfile, :filename => picture_path_params["filename"], :original_filename => picture_path_params["original_filename"]) | |
#replace picture_path with the new uploaded file | |
params[:picture][:picture_path] = uploaded_file | |
end | |
@picture = Picture.new(params[:picture]) | |
respond_to do |format| | |
if @picture.save | |
format.html { redirect_to @picture, notice: 'Picture was successfully created.' } | |
format.json { render json: @picture, status: :created, location: @picture } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @picture.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# picture {:user_id => "1", :folder_id => 1, etc., :picture_path {:file => "base64 awesomeness", :original_filename => "my file name", :filename => "my file name"}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment