调试帮助:Ruby on Rails S3 和 Carrierwave 文件保存失败
Debugging Help: Ruby on Rails File Saving Failed with S3 and Carrierwave
我一辈子都弄不明白为什么这行不通。我需要调试帮助,我是 rails 上 ruby 的新手。
我只想将一个简单的 s3 文件存储到我的亚马逊存储桶中。而已。我不需要像所有示例所示那样将该文件附加到控制器中的任何行或存储在数据库中。我只需要它把它放在桶里。我想我可能需要覆盖头像上传器中的 store_dir,但我不知道该怎么做。
下面是我的
观点:
= form_tag import_orders_path, :class => 'order-uploads', :multipart => true do
= file_field_tag 'upload[file]'
/ :file for just getting param[:file] from server
%br
= submit_tag "Import CSV", :class => 'submit-file'
控制者:
uploader = AvatarUploader.new
puts YAML::dump(params[:upload][:file].path)
uploader.store!(params[:upload][:file])
文件路径名称:
/tmp/RackMultipart20150115-9225-o2c5hp
avatar.rb 上传者
# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
# Choose what kind of storage to use for this uploader:
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
carrierwave.rb 初始化器
require 'fog'
require 'carrierwave'
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'my_key_id', # required
:aws_secret_access_key => 'my_secret_key', # required
:region => 'us-east-1', # optional, defaults to 'us-east-1'
}
config.fog_directory = "my_bucket"
end
错误:
NoMethodError (undefined method `id' for nil:NilClass):
app/uploaders/avatar_uploader.rb:16:in `store_dir'
app/controllers/orders_controller.rb:18:in `import'
NoMethodError (undefined method `id' for nil:NilClass):
app/uploaders/avatar_uploader.rb:16:in `store_dir'
app/controllers/orders_controller.rb:18:in `import'
你上传的文件应该是
应该是app/uploaders/avatar_uploader.rb
class AvatarUploader < CarrierWave::Uploader::Base
# Choose what kind of storage to use for this uploader:
storage :fog
end
然后
uploader = AvatarUploader.new
uploader.store!(my_file)
uploader.retrieve_from_store!('my_file.png')
我一辈子都弄不明白为什么这行不通。我需要调试帮助,我是 rails 上 ruby 的新手。
我只想将一个简单的 s3 文件存储到我的亚马逊存储桶中。而已。我不需要像所有示例所示那样将该文件附加到控制器中的任何行或存储在数据库中。我只需要它把它放在桶里。我想我可能需要覆盖头像上传器中的 store_dir,但我不知道该怎么做。
下面是我的
观点:
= form_tag import_orders_path, :class => 'order-uploads', :multipart => true do
= file_field_tag 'upload[file]'
/ :file for just getting param[:file] from server
%br
= submit_tag "Import CSV", :class => 'submit-file'
控制者:
uploader = AvatarUploader.new
puts YAML::dump(params[:upload][:file].path)
uploader.store!(params[:upload][:file])
文件路径名称:
/tmp/RackMultipart20150115-9225-o2c5hp
avatar.rb 上传者
# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
# Choose what kind of storage to use for this uploader:
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
carrierwave.rb 初始化器
require 'fog'
require 'carrierwave'
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'my_key_id', # required
:aws_secret_access_key => 'my_secret_key', # required
:region => 'us-east-1', # optional, defaults to 'us-east-1'
}
config.fog_directory = "my_bucket"
end
错误:
NoMethodError (undefined method `id' for nil:NilClass):
app/uploaders/avatar_uploader.rb:16:in `store_dir'
app/controllers/orders_controller.rb:18:in `import'
NoMethodError (undefined method `id' for nil:NilClass):
app/uploaders/avatar_uploader.rb:16:in `store_dir'
app/controllers/orders_controller.rb:18:in `import'
你上传的文件应该是
应该是app/uploaders/avatar_uploader.rb
class AvatarUploader < CarrierWave::Uploader::Base
# Choose what kind of storage to use for this uploader:
storage :fog
end
然后
uploader = AvatarUploader.new
uploader.store!(my_file)
uploader.retrieve_from_store!('my_file.png')