gpt4 book ai didi

ruby-on-rails - Rails 4、Paperclip 4.2.1 上传二进制文件时出错

转载 作者:行者123 更新时间:2023-12-02 01:37:58 24 4
gpt4 key购买 nike

我使用 rails 4 和 paperclip 4.2.1 进行了以下设置

class Post< ActiveRecord::Base
has_attached_file :key
allowed_content_type = ['text/plain',
'text/rtf',
'text/richtext',
'application/txt',
'application/octet-stream']
validates_attachment_content_type :key, :content_type => allowed_content_type, :message=> "Only #{allowed_content_type} is allowed"

我的 application.rb 中有这个

<body data-controller="<%= controller.controller_path %>" data-action="<%= controller.action_name %>" data-no-turbolink="true">
<%= content_tag "div", id: "params", data: { params: params } do %>
<% end %>

后 Controller 很简单

  def update
Post.transaction do
@post.attributes = (artifact_params)
if @artifact.errors.blank?
redirect_to(@artifact, :notice => 'Evidence item updated successfully')
else
render :action => 'edit'
raise ActiveRecord::Rollback
end

它可以完美地处理所有其他文件类型。当我尝试二进制文件时出错。这是错误:

Encoding::UndefinedConversionError in Posts#update

app/views/layouts/application.html.erb 第 58 行出现:

56: <body data-controller="<%= controller.controller_path %>" data-action="<%= 
57: controller.action_name %>" data-no-turbolink="true">
58: <%= content_tag "div", id: "params" , data: { params: params } do %>
59: <%#= params.inspect %>
60: <% end %>

在日志中它说:

ActionView::Template::Error ("\xAD" from ASCII-8BIT to UTF-8):
55: </head>
56:
57: <body data-controller="<%= controller.controller_path %>" data-action="<%= controller.action_name %>" data-no-turbolink="true">
58: <%= content_tag "div", id: "params" , data: { params: params } do %>
59: <%#= params.inspect %>
60: <% end %>
61:
app/views/layouts/application.html.erb:58:in `_app_views_layouts_application_html_erb__387563064_102572910'
app/controllers/posts_controller.rb:978:in `block in update'
app/controllers/posts_controller.rb:790:in `update'
Rendered /home/adminuser/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (15.0ms)
Rendered /home/adminuser/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (7.1ms)
Rendered /home/adminuser/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.9ms)
Rendered /home/adminuser/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (47.8ms)
Cannot render console with content type multipart/form-dataAllowed content types: [#<Mime::Type:0xa835748 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0xa835608 @synonyms=[], @symbol=:text, @string="text/plain">]

最佳答案

花了一整天的时间,我发现这是由回形针中的错误引起的。如果您没有将二进制文件映射到 application/octet-stream,它会在尝试将 params 转换为 View 主体中的 json 字符串时产生此错误。您必须将任何二进制文件类型映射到 application/octet-stream 才能消除此错误。

1.所以在config/initializers/中创建paperclip.rb2.在config/initializers/paperclip.rb中放置以下代码:

Paperclip.options[:content_type_mappings] = {
tc: 'application/octet-stream'
}

其中 tc 是二进制文件的扩展名。如果你有一个没有扩展名的文件,我不知道这将如何工作。回形针所有者应清楚地记录这一点,以免给用户带来痛苦。

关于ruby-on-rails - Rails 4、Paperclip 4.2.1 上传二进制文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29934438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com