I am following this tutorial which looks like its written for Rails 4 with aws-sdk version 1, I am using version 2, my controller is
class UploadsController < ApplicationController
def new
end
def create
bucket_name = 'my-first-bucket-tutorial986'
# file_name = params[:file][:tempfile].original_filename
# file_name = file_params.original_filename
file_name = File.basename( params[:file], File.extname(params[:file]) ).to_s
s3 = Aws::S3::Resource.new(options = {region: 'Ireland'})
s3.bucket(bucket_name).object('target').upload_file(file_name)
# obj = S3_BUCKET.objects[file_params.original_filename]
#obj = Aws::S3::Object.new(params[:file].original_filename, options = {bucket: 'my-first-bucket-tutorial986'})
# Upload the file
# obj.upload_file(params[:file], options = {acl: :public_read})
=begin obj.write(
file: params[:file],
# file: file_params,
acl: :public_read
)
=end
# Create an object for the upload
@upload = Upload.new(
url: obj.public_url,
name: obj.key
)
# Save the upload
if @upload.save
redirect_to uploads_path, success: 'File successfully uploaded'
else
flash.now[:notice] = 'There was an error'
render :new
end
end
def index
@uploads = Uploads.all
end
private
def file_params
params.require(:file) #.permit(:name, :age)
end
end
The error I am getting now is "no implicit conversion of ActionDispatch::Http::UploadedFile into String" but previously the path to the file could not be found when using the params[:file] or the file_params.original_filename
the view is
<h1>Upload a file</h1>
<%= form_tag uploads_path, enctype: 'multipart/form-data' do %>
<%= file_field_tag :file %>
<%= submit_tag 'Upload file' %>
<% end %>
Aucun commentaire:
Enregistrer un commentaire