che*_*ell 20 paperclip ruby-on-rails-3
我想在我的本地计算机上上传图像以进行开发,但是将它们存储在我的Amazon S3帐户上以进行生产.
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
Run Code Online (Sandbox Code Playgroud)
这里有一些代码重复.有没有办法在没有代码重复的情况下编写它.
这是解决方案非常感谢乔丹和安德烈在下面:
配置/环境/ development.rb
PAPERCLIP_STORAGE_OPTS = {
:styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>' },
:convert_options => { :all => '-quality 92' },
:processor => [ :cropper ]
}
Run Code Online (Sandbox Code Playgroud)
配置/环境/ 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 ]
}
Run Code Online (Sandbox Code Playgroud)
And*_*kov 17
另一个解决方案是将带有参数的散列移动到常量,这将在config/environments/*.rb文件中定义.然后你可以使用
has_attached_file :proto, PAPERCLIP_STORAGE_OPTS
Run Code Online (Sandbox Code Playgroud)
我认为在定义方法时使用if/unless在模型中有点混乱
Jor*_*ing 14
当然.尝试这样的事情:
paperclip_opts = {
:styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>' },
:convert_options => { :all => '-quality 92' },
:processor => [ :cropper ]
}
unless Rails.env.development?
paperclip_opts.merge! :storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ':attachment/:id/:style.:extension',
:bucket => 'birthdaywall_uploads',
end
has_attached_file :photo, paperclip_opts
Run Code Online (Sandbox Code Playgroud)
除了明显unless/ merge!块之外,还要注意使用:allfor :convert_options而不是指定相同的选项三次.