[英]File uploading to /tmp, rails 2
我想將文件上傳到/ tmp文件夾,然后在控制器中使用它。 但是文件不會從視圖轉到控制器。 看我的看法:
<% form_tag import_cash_payments_forms_path, {:method => :post, :multipart => true} do %>
<b>city:</b>
<%= select :data, :city_id, @cities %>
<br>
<br>
<b>region:</b>
<%= select :data, :region_id, @regions %>
<br>
<br>
<b>date:</b>
<%= date_select2 :data, :date %>
<br>
<br>
<b>file:</b><br>
<%= file_field_tag :file %><br>
<small>Доступные форматы: xml, xmlx</small>
<br>
<br>
<%= submit_tag "Load", :onclick => "submitAndTemporarilyDisable(this)" %>
<% end %>
這是我的控制器:
def import_cash_payments
selects
employer_id = current_employer.id
datas = params.slice(:city_id, :region_id, :date)
file = File.new(params[:file]) # HERE IS ERROR... File is nil, but why?
file.save
import = Import.new(datas, employer_id, file)
import.run
end
我得到的錯誤:
can't convert nil into String
在提交之前給出錯誤,我什至不能按下按鈕,因為cuz視圖不起作用。
這是我過去手動完成的操作(與使用Paperclip或類似工具相反)。 這樣做的關鍵是,您可以使用.read
從file參數中提取數據,然后使用.write
將其寫入正在創建的新文件中。 您也需要給它一個文件名,我在這里使用時間戳。
#you need to make a filename for the file in tmp. Let's use a timestamp
@filename = File.join("/tmp", Time.now.to_f)
@file = File.open(@filename,"w"){|f| f.write params[:file].read}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.