How to submit a form multiple time in Ruby on Rails - ruby-on-rails

I have a form made with simple_form that accept csv file to be converted it in an xlsx file (operating some operation to manipulate data).
<%= simple_form_for order, url: convert_orders_url, html: {multipart: true} do |f| %>
<%= f.file_field :file %>
<%= f.submit "Convert" %>
<% end %>
In my controller:
def convert
filename = #call to function that return the file path
send_file filename
end
The problem I have is that I need to upload multiple file one by one.
Anyway every time I need to reload the page manually cause I cannot submit the form multiple times and I can't reload the page in the controller because it raise a Double Render Error, because of the "send_file".
Is there a way to submit a form multiple time without reload the page in Ruby on Rails?

You can use remote: true on your form to submit the form using AJAX without reloading the page. However, with file uploads you'll find that remote: true won't work out of the box and you'll need something like remotipart.

Related

Rails - Converting ransack result to xls

I already tried:
<%= link_to "Download Excel File", yours_path(params.merge(:format => 'xls')) %>
which does not take into account the search form I have done on the same screen (it downloads everything).
Search form (works perfectly well, I get the right results on the page):
<%= search_form_for #q, url: yours_path do |f| %>
<div>
<%= f.search_field :last_name_cont, placeholder: "Type a last name" %>
</div>
I also already tried:
make xls
which tries to use the URL to only download the right results but it downloads everything.
Index:
def index
#q = Member.ransack(params[:q])
end
I would like to download only the result from ransack instead of all the objects.
Use this gem axlsx_rails for xlsx output and templates.
https://github.com/straydogstudio/axlsx_rails/blob/master/README.md

Post XML file content from client browser to server

I want to get xml file content on the server by Ruby on Rails.
How can i do that without upload file. and send xml file content from client to server in user browser.
exactly what I want to do. writing an app to get gpx file content from a form. user should enter file path in the form and submit.
This is what happens when you upload a file in Rails, i.e. with a form like
<%= form_for #page, multipart: true do |f| %>
<%= f.file_field :name %>
<%= f.submit %>
<% end %>
you get a ActionDispatch::Http::UploadedFile object in params[:page][:name]. This UploadedFile contains among others a File object called tempfile.
You can read and parse the content of the uploaded XML file from this tempfile
Normally you would use a gem like paperclip or carrierwave to handle file uploads.

Rails 3.0.1 set default file on a file_field

I have two rails applications which are connected through an API. In the first rails app I have many users with avatar images. Now I want to send them through a file_field (in form_for) to the second rails app.
The problem is that I don't want manualy select the image-path for each user. Better would be if the image_path would be automaticaly inserted when the page is loaded. At the moment there is for each user a page where I select his avatar image and click on send.
The location of the images is on the first rails server. So this means that I want to send avatar images from one rails server to another with a file_field.
Is that somehow possible?
If you are trying to set a default image when opening a record for editing you have to do the following
In your edit view:
<%= form_for(#photo, :html => { :multipart => true }) do |f| %>
<%= f.text_field :name %>
<%= f.file_field :image %>
<%= f.submit "Change" %>
<% end %>
In your controller:
#photo = Photo.find(params[:id])
When you form gets displayed it will automatically assign the image in the database to the file_field.
Although the form might show "No File Selected", When you update your form your original image will still be there.

error 406, remote: true Ajax form is still causing a redirect

This has had me stumped all day.
My app provides for the upload of documents, once uploaded it runs create.js.erb and adds the new record to the page.
My HTML looks like this,
<div id="doc_upload">
<%= form_for Document.new, remote: true do |f| %>
<%= f.file_field :file, multiple: true, name: "document[file]" %>
<%= f.hidden_field :plot_id %>
<%= f.submit 'upload' %>
<% end %>
</div>
I have a create.js.erb file in the documents directory. I am presently using JQuery file upload and the drag and drop file upload causes no issues. However when I use this form to manually select a file and upload I get:
a 406 HTTP error
the page redirects to /documents
when i go back, the file has been successfully uploaded and saved.
The headers are as follows.
Request URL:http://localhost:3000/documents
Request Method:POST
Status Code:406 Not Acceptable
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8}
my controller is:
def create
#document = Document.create(params[:document])
respond_to do |format|
format.js
end
end
As I said this really has me scratching my head, as the Ajax request doesnt seem to be firing. Does this mean that it may be something to do with the unobtrusive JS no picking up the remote true data type. That is the only reason that I can think of the redirection.
If so how I confirm and fix this?
Any help would be great.
Ross
You have to add :multipart => true if you have file upload in your form.
but you cannot post a multipart form via AJAX

Rails - Using ajax to upload images

I've been trying to upload an image using ajax, but maybe I'm doing it wrong
<%= simple_form_for #note, remote: true do |f|%>
<%= f.text_area :content%>
<%= f.file_field :picture %>
<%= f.submit "save"%>
<%end%>
but every time I hit save... it is not doing the upload, it goes to the show action instead of displaying the alerts that I have in the file create.js.erb (it does have the multipart parameter in the form label.
Is it possible to upload an image using this way? or should I check the jquery-file-upload library?
Thanks in advance
Javier
You can not use Ajax for a file upload. But you can use e.g. the fileupload plugin for jQuery http://blueimp.github.com/jQuery-File-Upload/
I had the same problem --> form_tag with remote: true does not make an ajax request
You can use jquery.form plugin for file upload through ajax. Here is a link for that.
http://jquery.malsup.com/form/

Resources