看起来 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 会自动添加它,但这并没有帮助。
在这一点上,我对替代解决方案持开放态度(希望允许我远程提交带有图像的表单)