Reputation: 491
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:
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
Upvotes: 4
Views: 891
Reputation: 8169
You have to add :multipart => true if you have file upload in your form.
but you cannot post a multipart form via AJAX
Upvotes: 4