Rails 5雾CarrierWave照片上传到亚马逊S3没有隐含的字符串转换为阵列

问题描述:

我是一个绝对的初学者的Ruby on Rails。 我遇到了一个错误,我不知道该如何处理。Rails 5雾CarrierWave照片上传到亚马逊S3没有隐含的字符串转换为阵列

我想做什么:将照片上传到Amazon S3存储桶。

错误:没有将数组隐式转换为字符串

的错误出来时,@ prototype.save原型#未能在prototypes_controller.rb创建行动

问:为了解决这个问题,时候正是我应该转换数组字符串?

显示在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"} 

以下是可能与错误有关的文件。

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 

配置/初始化/ 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 

回溯

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

如果有人遇到下面的错误,你能给我一些线索吗? 任何意见将不胜感激!提前致谢!

+0

请您附上该错误的回溯?它可能有助于解决您的问题。 – iskvmk

+0

我将它附加到上面的问题。我希望这会有所帮助。 – ILoveBaymax

,我建议你到里面添加CarrierWave.configure块下面几行:

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

它可以帮助获得更多的理解错误。

+0

谢谢你的建议。但是,出现同样的错误...将这些属性设置为false并不能提供更详细的信息...... – ILoveBaymax