【问题标题】:Rails 4 not using Remotipart to remotely submit formRails 4 不使用 Remotipart 远程提交表单
【发布时间】:2014-08-24 14:45:16
【问题描述】:

看起来 Remotipart 实际上并没有用于提交我的表单,所以当我查看表单提交到的参数时,图像完全被忽略了。

remotipart_submitted? 返回错误

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

下面是更相关的代码

宝石

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

Javascript

//= 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 %>

我在没有multipart: true 的情况下尝试过,因为我认为 form_for 会自动添加它,但这并没有帮助。

此时我对替代解决方案持开放态度(希望允许我远程提交带有静止图像的表单)

【问题讨论】:

  • 您找到解决方案了吗?我遇到了同样的问题。 :(
  • @SwaathiK 不完全是,我在他们的 Github 上添加了一个问题,但他们从未回应。我在那里提供了一个替代库:github.com/JangoSteve/remotipart/issues/109
  • 太棒了!谢谢。但我只是尝试使用没有任何宝石的远程真。我有 jquery 文件上传虽然。它似乎起作用了。表单通过 AJAX 提交。这是正确的吗?
  • 我面临同样的问题并尝试了github.com/JangoSteve/remotipart/issues/109 中的任何建议。我离解决方案还很远。
  • 好的,我不知道为什么 github 问题中没有答案,但是当我尝试使用与 Rails 4.2.6 完全相同的字段时,这似乎对我有用。 @TMP 你能检查它是否适合你吗?或者让我知道我该怎么做才能证明它有效?

标签: jquery forms ruby-on-rails-4 ajaxform remotipart


【解决方案1】:

下面的代码可以让我通过带有图像的 ajax 提交表单。即使它的 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>

我希望这对面临问题的人有用。

【讨论】:

    猜你喜欢
    • 2012-02-28
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 2012-05-07
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多