簡體   English   中英

在Rails中將文件提交到控制器上載操作的表單

[英]Form to submit file to controller upload action in 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

我正在嘗試創建可以從用戶硬盤驅動器中選擇文件並上傳的表格。

您需要使用參數。 存放所有變量的位置。

像這樣:

  params[:file]

但是對於使用文件上傳 (略有不同),請查看文檔

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM