表单以提交文件到rails中的控制器上载操作

时间:2013-09-01 12:38:22

标签: ruby-on-rails

控制器:

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

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

1 个答案:

答案 0 :(得分:1)

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

像这样:

  params[:file]

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

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