Reputation: 7921
It looks like Remotipart isn't actually being used to submit my form, so the image is completely left out when I look at the params where the form gets submitted to.
remotipart_submitted?
returns false
params: {"utf8"=>"✓", "product"=>{"name"=>"RemotipartFails", "price"=>"10", "description"=>"Please work"}, "action"=>"create", "controller"=>"products"}
Below is more relevant code
gem "jquery-rails"
gem "remotipart", "~> 1.2"
//= require jquery
//= require jquery_ujs
//= require jquery.remotipart
<%= form_for(:product, url: products_path, remote: true, html: { multipart: true, class: "form-horizontal" }) do |f| %>
<div class="margin-top-10 margin-bottom-10">
<div class="input-left">
<%= f.text_field :name, { placeholder: "Name", class: "form-control" } %>
</div>
<div class="input-right">
<%= f.number_field :price, { placeholder: "Price", class: "form-control" } %>
</div>
<div class="clearfix margin-bottom-10"></div>
<div class="input-full">
<%= f.text_field :description, { placeholder: "Description", class: "form-control" } %>
</div>
<div class="clearfix margin-bottom-10"></div>
<div class="input-full">
<%= f.file_field :image, { class: "form-control" } %>
</div>
<div class="clearfix margin-bottom-10"></div>
<%= f.submit "Add Product", class: "btn btn-green" %>
</div>
<% end %>
I've tried it without the multipart: true
because I think form_for adds it automatically, but that didn't help.
At this point I'm open to alternative solutions (hopefully allowing me to submit the form remotely with an image still)
Upvotes: 9
Views: 1649
Reputation: 3790
Bellow code working for me to submit the form via ajax with image. Even though its form_tag, It can be easily changed to form_for syntax.
<!--In View-->
<%= javascript_include_tag "jquery", "jquery_ujs", "jquery.remotipart"%>
<%= form_tag({:controller=>controller,:action=>action},{:method => 'post' ,:name=>'upload-data',:id=>'upload-data', :multipart => true, :target =>'upload_frame' }) do -%>
<!--Form fields here-->
<%end %>
<script type="text/javascript">
$("#upload_frame").load(function() {
var responseText = $("#upload_frame").contents().find('body').html();
if(responseText != ""){
$("#element_to_update").html(responseText);
}
});
</script>
I hope this will be useful to someone facing issue.
Upvotes: 0