简体   繁体   中英

What is the content type of json file to AWS S3 with ruby?

I'd like to upload json file to S3 by ruby with paperclip . I coded as following, but it returned the following error.

undefined method `merge' for "application/json":String

Could you tell me how to set content-type of json-file?

product_definition.rb

class ProductDefinition < ActiveRecord::Base
  belongs_to :product

  has_attached_file :meta_data,
    :storage => :s3,
    :s3_credentials => "#{Rails.root}/config/s3.yml",
    :url => ":s3_domain_url",
    :path => "/assets/:id/:style/:basename.:extension",
    :s3_host_name => "s3-ap-northeast-1.amazonaws.com"

  validates_attachment :meta_data, content_type: 'application/json'
end

products_controller.rb

@product_definition = ProductDefinition.create(meta_data:sample.json)

If you're using a recent version of Paperclip it looks like you formatted the arguments to validates_attachment_content_type incorrectly. I think it should be:

validates_attachment :meta_data, content_type: { content_type: 'application/json' }

See the example in the Paperclip Readme: https://github.com/thoughtbot/paperclip#validations

EDIT: How I came to this conclusion is by noticing the error says it tried to call merge on the string. Merge is meant to be called on a Hash, so therefore it expects a Hash value for :content_type .

What you have is exactly what the current docs on github state, but it does not work: https://github.com/thoughtbot/paperclip Other examples use a different way of formatting matching types - so very confusing.

What does seem to work, is a syntax-change which appears to date back years, which is:

validates_attachment :meta_data, attachment_content_type: {content_type: 'application/json'}

You can also put a set of acceptable content-types in an array, such as

validates_attachment :meta_data, attachment_content_type: {content_type: ['application/json', 'application/doc']}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM