看起来Remotipart实际上并没有被用来提交我的表单,因此当我查看提交表单的params时,图像完全被遗漏了.
remotipart_submitted? 返回false
PARAMS: {"utf8"=>"?", "product"=>{"name"=>"RemotipartFails", "price"=>"10", "description"=>"Please work"}, "action"=>"create", "controller"=>"products"}
下面是更相关的代码
gem "jquery-rails"
gem "remotipart", "~> 1.2"
Run Code Online (Sandbox Code Playgroud)
//= require jquery
//= require jquery_ujs
//= require jquery.remotipart
Run Code Online (Sandbox Code Playgroud)
<%= 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"> …Run Code Online (Sandbox Code Playgroud) 当尝试通过Carrierwave和Rails 5远程上传图像时,我遇到了编码错误:
编码:: UndefinedConversionError - 从ASCII-8BIT到UTF-8的"\ x89"
我已经看过这个答案以及答案提出的activesupport-json_encoder gem,但没有运气.宝石似乎不支持导轨5.
我的表单是一个正常的多部分rails表单,如下所示:
= bootstrap_form_for @image, url: members_profile_path(current_user), remote: true, html: { multipart: true } do |f|
= f.file_field :attachment
= f.button "Save"
Run Code Online (Sandbox Code Playgroud)
包含图像的字段称为"附件".在我的控制器中,我的代码如下所示:
def create_profile_image
respond_to do |format|
@image.entity_id = current_user.id
if @image.validate(params[ :image ])
@image.save
format.json { render json: @image, status: :ok }
else
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud)
我正在使用改革宝石处理允许的参数.我确保允许附件参数.此外,我还添加了Remotipart gem,以通过Rails简化远程文件上传.
任何想法,为什么我可能会出现此错误?
UPDATE
这是我在日志中可以看到的:
Started …Run Code Online (Sandbox Code Playgroud) Rails 5 和载波
我加:
gem 'remotipart', '~> 1.3.1'
Run Code Online (Sandbox Code Playgroud)
和
//= require jquery.remotipart
Run Code Online (Sandbox Code Playgroud)
我有这样的表格:
= simple_form_for state, remote: true do |f|
= f.input :wsr
Run Code Online (Sandbox Code Playgroud)
该表单的输出:
<form class="simple_form" novalidate="novalidate" id="edit_odc_state_73" enctype="multipart/form-data" action="/odc_states/73" accept-charset="UTF-8" data-remote="true" method="post">
Run Code Online (Sandbox Code Playgroud)
但是当我提交表单时:
Started PATCH "/odc_states/73" for 127.0.0.1 at 2016-12-29 16:02:56 +0700
Processing by OdcStatesController#update as HTML
Parameters: {"utf8"=>"?", "odc_state"=>{"wsr"=>#<ActionDispatch::Http::UploadedFile:0x007f67ba393338 @tempfile=#<Tempfile:/tmp/RackMultipart20161229-10725-1r3sn5z.txt>, @original_filename="test1.txt", @content_type="text/plain", @headers="Content-Disposition: form-data; name=\"odc_state[wsr]\"; filename=\"test1.txt\"\r\nContent-Type: text/plain\r\n">}, "id"=>"73"}
Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
Run Code Online (Sandbox Code Playgroud)
为什么以 HTML 格式发送,有什么想法吗?
我正在使用Remotipart上传文件.Rails-Controller处理文件,但我无法弄清楚如何获得以下ajax响应.这是我的js代码.
$file.children(".description").html(
'<%= form_for FileObject.new, :url => file_object_index_path , :html => { :multipart => true }, :remote => true do |f| %>' +
'<div class="field">' +
'<%= f.label :file %>' +
'<%= f.file_field :file %>'+
'</div>' +
'<input type="hidden" name="directory_object_id" value="' + current_directory.id +'" />' +
'<div class="actions">' +
'<%= f.submit %>' +
'</div>' +
'<% end %>'
);
$("form").bind('ajax:success', function(){
alert("success");
});
Run Code Online (Sandbox Code Playgroud)
也许有人之前已经解决过这个