Reputation: 313
I want to upload an file to /tmp folder, then use it in controller. But a file do not goes from view to controller. Look my view:
<% 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 %>
And here is my controller:
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
Error that I get:
can't convert nil into String
Error is given befor submit, i cant even press button, cuz view doesnt work.
Upvotes: 0
Views: 337
Reputation: 32955
Here's what i've done in the past, to do just this by hand (as opposed to using Paperclip or similar). The key to this is that you pull the data from the file parameter with .read
and then write it into the new file you're creating with .write
. You need to give it a filename too, i'm using a timestamp here.
#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}
Upvotes: 1