Rails 4, Paperclip 4.2.1 give error while uploading binary

I have the following setup with 4 rails and 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"

      

I have this in my .rb application

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

      

The mail controller is simple

  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

      

It works flawlessly with all other file types. Errors when trying a binary file. This is mistake:

Coding :: UndefinedConversionError in post update #

app / views / layouts / application.html.erb where line # 58 is raised:

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

      

The log says:

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">]

      

+3


source to share


1 answer


After spending all day on this, I realized it was caused by a paperclip error. If you don't have a binary associated with the application / octet stream, it throws this error when trying to convert the parameters to json string in the body of the view. You must match any type of binary to an application / octet stream to get rid of this error.

1. Create a paperclip.rb file in config / initializers / 2. In config / initializers / paperclip.rb, place the following code:



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

      

where tc is the extension of your binary. I don't know how this would work if you have a file without an extension. clip owners must document this to save the pain of users.

+2


source







All Articles