简体   繁体   English

在Rails中将文件提交到控制器上载操作的表单

[英]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 http://guides.rubyonrails.org/form_helpers.html#uploading-files

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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