我是一個絕對的初學者的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'
如果有人遇到下面的錯誤,你能給我一些線索嗎? 任何意見將不勝感激!提前致謝!
請您附上該錯誤的回溯?它可能有助於解決您的問題。 – iskvmk
我將它附加到上面的問題。我希望這會有所幫助。 – ILoveBaymax