我尝试使用carrierwave和fog gem在s3上传照片,但它有错误。
undefined method
捕获'为nil:NilClass .
in line :
如果@ premium.save`
在控制器/动作创建
def create
@premium = Article.new(premium_params)
@premium.created_date = Time.now
if @premium.save
redirect_to admincp_premium_path(@premium.id)
else
respond_to do |format|
format.html { render :new }
format.json { render json: @premium.errors, status: :unprocessable_entity }
end
end
端
这是我的代码:
carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => ENV['S3_KEY'],
:aws_secret_access_key => ENV['S3_SECRET'],
:region => ENV['S3_REGION'],
:path_style => true
}
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory = ENV['S3_BUCKET']
end
在article_controller.rb
中def create
@premium = Article.new(premium_params)
@premium.created_date = Time.now
if @premium.save
redirect_to admincp_premium_path(@premium.id)
else
respond_to do |format|
format.html { render :new }
format.json { render json: @premium.errors, status: :unprocessable_entity }
end
end
end` 在模型artcle.rb
Class Article < ActiveRecord::Base
...
# set file upload image
mount_uploader :image, ImageUploader
end.
感谢您拯救我的生命:D。
错误日志:
Started POST "/admincp/premiums/new" for 127.0.0.1 at 2015-03-30 16:55:53 +0700
Processing by Admincp::PremiumsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"m38F1BDY50lGd/oueT4swpawWueiOBBgOT/MK7xHX4q9aGchBjEc1UnX8UEsTvE+rEz+F3Le9Ru1zVusx7W3dQ==", "premium"=>{"title"=>"rwe", "url"=>"wewe", "start_date"=>"01-03-2015", "end_date"=>"11-03-2015", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fd1b8d97c90 @tempfile=#<Tempfile:/tmp/RackMultipart20150330-11636-1pd05hk.jpg>, @original_filename="canh-dep-tai-nha-trang.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"premium[image]\"; filename=\"canh-dep-tai-nha-trang.jpg\"\r\nContent-Type: image/jpeg\r\n">, "number"=>"10"}, "commit"=>"登録"}
(0.2ms) BEGIN
SQL (0.8ms) INSERT INTO `article` (`title`, `url`, `image`, `start_date`, `end_date`, `number`, `created_date`) VALUES ('rwe', 'wewe', '1d5f0c8e-c41e-4eda-a379-b1b51430372a.jpg', '2015-03-01 00:00:00.000000', '2015-03-11 00:00:00.000000', 10, '2015-03-30 09:55:53.363046')
[fog][WARNING] fog: followed redirect to s3.amazonaws.com, connecting to the matching region will be more performant
(549.1ms) ROLLBACK
Completed 500 Internal Server Error in 7494ms
NoMethodError (undefined method `captures' for nil:NilClass):
app/controllers/admincp/premiums_controller.rb:32:in `create'
答案 0 :(得分:0)
我遇到了同样的问题。我可以通过调整CarrierWave :region
中的config
来解决此问题。
我在我的存储区中使用了S3的US Standard
区域,并设置了region: 'us-east-1'
,这是默认值。当我之前在Oregon
设置region: 'us-west-2'
工作时使用了一个存储桶(CarrierWave/fog.rb中的区域选项中未列出的选项。)