简体   繁体   中英

How can I upload image in S3 by carrierwave and save filename in database?

I tried to upload photo in s3 using carrierwave and fog gem but it had error.

undefined method captures' for nil:NilClass . in line : . in line : if @premium.save` in controller/action create

  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

This is my code:

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

In 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` In model artcle.rb

Class Article < ActiveRecord::Base
   ...
   # set file upload image
  mount_uploader :image, ImageUploader
end.

Thanks for saving my life :D.

error log:

    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'

I had the same problem. I was able to resolve it by adjusting the :region in my CarrierWave config .

I'm using S3's US Standard region for my bucket, and I set region: 'us-east-1' , which is the default. When I previously used a bucket in Oregon setting region: 'us-west-2' worked (an option which is not listed in the region options in the CarrierWave/fog.rb .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM