1

控制器:

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

在索引视图中上传表单:

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

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

错误(已编辑):

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

我正在尝试创建此表单,该表单可以从用户硬盘驱动器中选择文件并上传。

4

1 回答 1

1

您需要使用参数。存储所有变量的地方。

像这样:

  params[:file]

但是对于使用文件上传 (略有不同),请查看文档

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

于 2013-09-01T12:40:39.703 回答