简体   繁体   English

Rails 5 Fog CarrierWave 照片上传到 Amazon S3 没有将字符串隐式转换为数组

[英]Rails 5 Fog CarrierWave Photo Upload to Amazon S3 no implicit conversion of String into Array

I am an absolute beginner of Ruby on Rails.我是 Ruby on Rails 的绝对初学者。 There is an error that I encountered and do not know to deal with.我遇到了一个错误,不知道如何处理。

WHAT I'D LIKE TO DO: Uploading a photo to Amazon S3 bucket.我想要做什么:将照片上传到 Amazon S3 存储桶。

Error: no implicit conversion of Array into String .错误:没有将 Array 隐式转换为 String

The Error comes out when @prototype.save fails in prototype#create action in prototypes_controller.rb当@prototype.save 在prototypes_controller.rb 中的prototype#create 操作失败时出现错误

Question: In order to solve this issue, when exactly should I convert Array into String?问题:为了解决这个问题,我应该在什么时候将 Array 转换为 String?

The params displayed in the Webrick server. Webrick 服务器中显示的参数。

{"utf8"=>"✓",
 "prototype"=>
   {"name"=>"dafda",
    "prototype_images_attributes"=>
      {"0"=>
        {"content"=>
           #<ActionDispatch::Http::UploadedFile:0x007fe07491b778
           @content_type="image/jpeg",
           @headers="Content-Disposition: form-data; name=\"prototype[prototype_images_attributes][0][content]\"; filename=\"card100002057_1.jpg\"\r\nContent-Type: image/jpeg\r\n",
           @original_filename="card100002057_1.jpg",
           @tempfile=#<File:/var/folders/yy/42bpgmdj4t16rf0_5xsly3tc0000gp/T/RackMultipart20170815-70968-fzdoey.jpg>>,
        "status"=>"main"},
       "1"=>{"status"=>"sub"},
       "2"=>{"status"=>"sub"}},
    "catchcopy"=>"fdafda",
    "concept"=>"fdafda"},
  "commit"=>"Publish"}

Here are files that might have to do with the error.以下是可能与错误有关的文件。

prototype_controller.rb原型控制器.rb

def create
  @prototype = current_user.prototypes.new(prototype_params)

  if @prototype.save
    flash[:notice] = 'Prototype was successfully created.'
    redirect_to root_path
  else
    flash[:alert] = 'Prototype was not successfully created.'
    render :new
  end
end

private
def prototype_params
  params.require(:prototype)
    .permit(:name,
            :catchcopy,
            :concept,
            prototype_images_attributes: [:content, :status]
    )
end

def set_prototype
  @prototype = Prototype.find(params[:id]).decorate
end

prototype_image_uploader.rb prototype_image_uploader.rb

class PrototypeImageUploader < CarrierWave::Uploader::Base
  storage :fog

  def store_dir
    'uploads'
  end

  def extension_whitelist
    %w(jpg jpeg gif png)
  end
end

config/initializer/carrierwave.rb配置/初始化程序/carrierwave.rb

require 'carrierwave/storage/abstract'
require 'carrierwave/storage/file'
require 'carrierwave/storage/fog'

CarrierWave.configure do |config|
  config.storage = :fog
  config.fog_credentials = {
    provider: 'AWS',
    aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
    aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
    region: 'ap-northeast-1'
  }

  case Rails.env
    when 'production'
      config.fog_directory = ENV['S3_BUCKET'],
      config.asset_host = ENV['S3_BUCKET_HOST']
    when 'development'
      config.fog_directory = ENV['S3_BUCKET'],
      config.asset_host = ENV['S3_BUCKET_HOST']
    when 'test'
      config.storage = :file
  end
end

Backtrace回溯

{"utf8"=>"✓",
 "prototype"=>
   {"name"=>"dafda",
    "prototype_images_attributes"=>
      {"0"=>
        {"content"=>
           #<ActionDispatch::Http::UploadedFile:0x007fe07491b778
           @content_type="image/jpeg",
           @headers="Content-Disposition: form-data; name=\"prototype[prototype_images_attributes][0][content]\"; filename=\"card100002057_1.jpg\"\r\nContent-Type: image/jpeg\r\n",
           @original_filename="card100002057_1.jpg",
           @tempfile=#<File:/var/folders/yy/42bpgmdj4t16rf0_5xsly3tc0000gp/T/RackMultipart20170815-70968-fzdoey.jpg>>,
        "status"=>"main"},
       "1"=>{"status"=>"sub"},
       "2"=>{"status"=>"sub"}},
    "catchcopy"=>"fdafda",
    "concept"=>"fdafda"},
  "commit"=>"Publish"}
User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
     BEGIN
   SQL (0.3ms)  INSERT INTO `prototypes` (`name`, `catchcopy`, 
`concept`, `created_at`, `updated_at`, `user_id`) VALUES ('fdfad', 
 'fdfad', 'fdfd', '2017-08-15 13:05:29', '2017-08-15 13:05:29', 2)
  SQL (0.2ms)  INSERT INTO `prototype_images` (`content`, 
 `prototype_id`, `created_at`, `updated_at`, `status`) VALUES 
 ('13248473_1049158575176323_1670750882476661352_o.jpg', 56, '2017-08-
 15 13:05:29', '2017-08-15 13:05:29', 0)
 (0.6ms)  ROLLBACK
 Completed 500 Internal Server Error in 32ms (ActiveRecord: 2.0ms)

 TypeError (no implicit conversion of Array into String):

 app/controllers/prototypes_controller.rb:24:in `create'

If anyone has encountered the error below, can you give me some clues?如果有人遇到下面的错误,你能给我一些线索吗? Any advice would be appreciated!任何建议将不胜感激! Thanks in advance!提前致谢!

I suggest you to add following lines inside CarrierWave.configure block:我建议您在CarrierWave.configure块中添加以下几行:

  config.ignore_integrity_errors = false
  config.ignore_processing_errors = false
  config.ignore_download_errors = false

It could help to get more understandable error.它可以帮助获得更容易理解的错误。

Take a look at the line where the fog_directory is being set看一下设置fog_directory

config.fog_directory = 'my_bucket_name',

See that comma at the end?看到最后那个逗号了吗? It turns it into an array.它把它变成一个数组。

Easy mistake to make when the fog_credentials bit is set to an object which requires commas at the end of every line.fog_credentials位设置为需要在每行末尾使用逗号的对象时,很容易犯错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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