[英]Rails 5 Fog CarrierWave Photo Upload to Amazon S3 no implicit conversion of String into Array
我是 Ruby on Rails 的絕對初學者。 我遇到了一個錯誤,不知道如何處理。
我想要做什么:將照片上傳到 Amazon S3 存儲桶。
錯誤:沒有將 Array 隱式轉換為 String 。
當@prototype.save 在prototypes_controller.rb 中的prototype#create 操作失敗時出現錯誤
問題:為了解決這個問題,我應該在什么時候將 Array 轉換為 String?
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"}
以下是可能與錯誤有關的文件。
原型控制器.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'
如果有人遇到下面的錯誤,你能給我一些線索嗎? 任何建議將不勝感激! 提前致謝!
我建議您在CarrierWave.configure
塊中添加以下幾行:
config.ignore_integrity_errors = false
config.ignore_processing_errors = false
config.ignore_download_errors = false
它可以幫助獲得更容易理解的錯誤。
看一下設置fog_directory
行
config.fog_directory = 'my_bucket_name',
看到最后那個逗號了嗎? 它把它變成一個數組。
當fog_credentials
位設置為需要在每行末尾使用逗號的對象時,很容易犯錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.