I seem to be stuck on figuring out the best approach to let a user upload a text file, have my application parse the file, and then return some output via ajax directly back on the same page. Is there an easy way to do this?
I have been using the following and it works wonderfully but I can’t get it to work with ajax.
Here is the view file – index.html.erb
<%= form_tag 'log/new', :multipart => true do %>
<label for="file">Please upload a log file to continue</label> <%= file_field_tag "file" %>
<%= submit_tag ('Upload')%>
<% end %>
I am then able to play with the results in my controller using params[:file] just fine but when I add :remote => true to my form it won’t let me do any AJAX or run my .js.erb view file, presumably for security reasons since the js can’t read the XHR request yadda yadda.
I have also tried CarrierWave and jquery-fileupload-rails gems but I don’t want to save it to the database first which carrier wave seemed to want to do and jquery-fileupload seems to do the same. In any event, even if I get the file uploaded/saved to db with one of those gems – how am I able to access the contents like I was able to with my params[:file] approach?
If you want to upload file with AJAX and rails forms you can use
remotipartgem:For me it works with Rails 3.2 as well.
https://github.com/JangoSteve/remotipart
http://rubygems.org/gems/remotipart