Tôi muốn tải hình ảnh lên máy cục bộ của mình để phát triển nhưng lưu trữ chúng trên tài khoản Amazon S3 của tôi để sản xuất.lưu trữ hình ảnh cục bộ để phát triển s3 cho sản xuất Rails Paperclip
upload.rb
if Rails.env.development?
has_attached_file :photo, :styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>'},
:convert_options => { :thumb => "-quality 92", :medium => "-quality 92", :large => "-quality 92" },
:processors => [:cropper]
else
has_attached_file :photo, :styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>'},
:convert_options => { :thumb => "-quality 92", :medium => "-quality 92", :large => "-quality 92" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'birthdaywall_uploads',
:processors => [:cropper]
end
Có một số mã lặp lại ở đây. Có cách nào để viết điều này mà không cần sao chép mã.
Dưới đây là giải pháp Cảm ơn lớn thời gian để Jordan và Andrey dưới đây:
config/môi trường/development.rb
PAPERCLIP_STORAGE_OPTS = {
:styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>' },
:convert_options => { :all => '-quality 92' },
:processor => [ :cropper ]
}
config/môi trường/production.rb
PAPERCLIP_STORAGE_OPTS = {
:styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>' },
:convert_options => { :all => '-quality 92' },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ':attachment/:id/:style.:extension',
:bucket => 'birthdaywall_uploads',
:processor => [ :cropper ]
}
Nhờ Jordan. Tôi sẽ làm như bạn đề xuất và cũng kết hợp ý tưởng ở trên để tôi có thể loại bỏ câu lệnh trừ. – chell