简体   繁体   中英

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

Error: no implicit conversion of Array into String .

The Error comes out when @prototype.save fails in prototype#create action in prototypes_controller.rb

Question: In order to solve this issue, when exactly should I convert Array into String?

The params displayed in the Webrick server.

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

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

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

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:

  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

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.

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