简体   繁体   中英

Form to submit file to controller upload action in rails

Controller:

class SongsController < ApplicationController
    .
    .
    .

    def upload
    bucket = find_bucket
    file = params[:file] edit: previously was file = :file
    if bucket           
        AWS::S3::S3Object.store(file, open(file), bucket)
        redirect_to root_path
    else
        render text: "Couldn't upload"
    end
end  


    private

    .
    .
    .

    def find_bucket
    AWS::S3::Bucket.find('kanesmusic')
end                     
end

Upload form in index view:

<h2>Upload a new MP3:</h2>  

<%= form_tag upload_path, :method => "post", :multipart => true do %>  
    <%= file_field_tag :file %>  
    <%= submit_tag "Upload" %>  
<% end %>

Error (edited):

TypeError in SongsController#upload...can't convert AWS::S3::Bucket into String

I'm trying to create this form that can select a file from a users hard drive and upload it.

You need to use params. That where all the vars are stored.

Like so:

  params[:file]

But for using file upload (thats slightly different) have a look at the docs

http://guides.rubyonrails.org/form_helpers.html#uploading-files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM