【问题标题】:Html form does not include in post's parameters file dataHtml 表单不包含在 post 的参数文件数据中
【发布时间】:2013-08-05 06:02:51
【问题描述】:

由于我什至无法猜测的原因,我的 rails 应用程序不再工作了。我没有更改 Rails 代码中的任何内容,所以我想这可能与浏览器有关。

简单表单,由 Simple_form 生成:

<%= simple_form_for @order, :url => fillparameters_order_path(@order), :remote => true do |f| %>

  <%= f.association :aspectRatio,  collection: @order.project.aspect_ratios %>
  <%= f.input :client_email, :as => :string %>
  <%= f.input :soundtrack, :as => :file %>
  <%= f.input :project_id, :as => :hidden %>
  <%= f.button :submit %>
<% end %>

它会生成以下内容:

form id="edit_order_4" class="simple_form edit_order" novalidate="novalidate" method="post" enctype="multipart/form-data" data-remote="true" action="/orders/4/fillparameters" accept-charset="UTF-8"
    <input id="order_soundtrack" class="file optional" type="file" name="order[soundtrack]">
    <input class="btn" type="submit" value="Send!" name="commit">

当然除了其他输入。

但是在点击提交按钮后,Firebug 控制台和 webrick 日志显示发送的所有内容都是除了我的文件字段之外的任何内容:

Started PUT "/orders/4/fillparameters" for 127.0.0.1 at 2013-08-04 21:00:03 +0900
Processing by OrdersController#fillparameters as JS
Parameters: {
    "utf8" => "✓",
    "authenticity_token" => "VpddWLzi7Czzl0L+gbd5wVfjbJ1pQnfI53L86BwbH/E=",
    "order" => {
        "aspect_ratio_id" => "1",
        "client_email" => "****@gmail.com",
        "project_id" => "1"
    },
    "commit" => "Send!",
    "id" => "4"
}

有什么想法吗,伙计们?

【问题讨论】:

    标签: html ruby-on-rails forms file post


    【解决方案1】:

    事实是:你不能在 rails 中使用 :remote => true 选项上传文件。有两种方法可以让您使用 AJAX 进行文件上传:

    1. rails 方式:使用 gem remotepart
    2. jquery方式:使用jquery文件上传插件,这样的插件很多,比如 https://github.com/blueimp/jQuery-File-Upload

      http://malsup.com/jquery/form/ 等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 2022-10-24
      相关资源
      最近更新 更多