I have a form that is attempting to read in a JSON file for parsing/actions/etc. I'm having problems getting it to read in the controller.
View:
<%= form_tag({:controller => :admins, :action => :upload_json}, {:multipart => true, :method => :post}) do |f| %>
<%= file_field_tag 'datafile' %>
<%= submit_tag "Upload" %>
Controller:
def upload_json
file_data = params[:datafile]
File.read(file_data) do |file|
file.each do |line|
## does stuff here....
end
end
end
A similar function works in my seed.rb
file when I'm seeding data - just can't get it to read in an uploaded file.
The error I'm getting is: can't convert ActionDispatch::Http::UploadedFile into String
.
Thanks in advance for the help!
Figured it out. Needed to change:
to
And decided to use the
.open
function to change:to
Open the uploaded file using
path
.params[:datafile]
is an instance ofActionDispatch::Http::UploadedFile
class and you'll need to get at the stored file by callingpath
to properly process it.Additionally,
File.read
will not get you the line-by-line processing you're looking for. You need to change that toFile.open
.Try this:
Controller
Alternative Style
params[:datafile] is an instance of ActionDispatch::Http::UploadedFile class with tempfile attached with that.To open the tempfile
You try something like