简体   繁体   English

Rails 4不使用Remotipart远程提交表单

[英]Rails 4 not using Remotipart to remotely submit form

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实际上并没有被用来提交我的表单,因此当我查看提交表单的params时,图像完全被遗漏了。

remotipart_submitted? returns false 返回false

params: {"utf8"=>"✓", "product"=>{"name"=>"RemotipartFails", "price"=>"10", "description"=>"Please work"}, "action"=>"create", "controller"=>"products"} params: {"utf8"=>"✓", "product"=>{"name"=>"RemotipartFails", "price"=>"10", "description"=>"Please work"}, "action"=>"create", "controller"=>"products"}

Below is more relevant code 下面是更相关的代码

Gems 宝石

gem "jquery-rails"
gem "remotipart", "~> 1.2"

Javascript 使用Javascript

//= require jquery
//= require jquery_ujs
//= require jquery.remotipart

Form 形成

<%= 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. 我已经尝试过没有multipart: true因为我认为form_for会自动添加它,但这没有帮助。

At this point I'm open to alternative solutions (hopefully allowing me to submit the form remotely with an image still) 在这一点上,我对替代解决方案持开放态度(希望允许我使用图像远程提交表单)

Bellow code working for me to submit the form via ajax with image. Bellow代码为我工作,通过带图像的ajax提交表单。 Even though its form_tag, It can be easily changed to form_for syntax. 即使它的form_tag,也可以很容易地将其更改为form_for语法。

<!--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. 我希望这对面临问题的人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM