问题
I'm having a similar problem to this issue, except I'm unable to modify the path at all.
I'm using Spree 2.2 which uses paperclip 3.4.2
I'm trying to modify the paperclip defaults to change the image path. All of the other configuration is working.
Myapp::Application.configure do
config.paperclip_defaults = {
:storage => :s3,
:s3_protocol => 'https',
:s3_host_name => "s3-eu-west-1.amazonaws.com",
:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension",
:url => "/:attachment/:id/:style/:basename.:extension",
:s3_credentials => {
:bucket => 'xxx',
:access_key_id => "xxx",
:secret_access_key => "xxx"
}
}
end
But with this code the URL is like so:
https://s3-eu-west-1.amazonaws.com/bucketname/home/username/path/to/project/public/spree/products/24/original/ror_baseball.jpeg?1390110939
I've tried adding the following to that config block:
config.attachment_path = '/spree/products/:id/:style/:basename.:extension'
I've also tried adding the following to the config/initializers/paperclip.rb
Paperclip::Attachment.default_options[:url] = "/:attachment/:id/:style/:basename.:extension"
Paperclip::Attachment.default_options[:path] = ":rails_root/public/:attachment/:id/:style/:basename.:extension"
And also tried:
Paperclip::Attachment.default_options.merge!(
:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension",
:url => "/:attachment/:id/:style/:basename.:extension"
)
Any ideas?
UPDATE: opened ticket on github
回答1:
For Spree 2.2 (not 2.1), use the following code in initializers/spree.rb:
Spree::Image.attachment_definitions[:attachment][:url] = ':path'
Spree::Image.attachment_definitions[:attachment][:path] = '/spree/products/:id/:style/:basename.:extension'
The rest of the configuration should be set as 2.1 (see here for details).
回答2:
I believe the /home/username/path/to/project/ part is being added by the :rails_root portion of your :path configuration.
If you look at the source code at: https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/attachment.rb
You will see that the default values are as follows:
:path => ":rails_root/public:url",
:url => "/system/:class/:attachment/:id_partition/:style/:filename",
What if you set the following:
:path => ":url",
:url => "/spree/products/:id/:style/:basename.:extension",
Let me know what these do for you and we can see what else you need to do in order to get this working properly.
回答3:
After a quick Google search, prompted by your comment I figured it out. I believe the answer is here, in the Spree documentation:
http://guides.spreecommerce.com/user/configuring_images.html
UPDATE: Apparently This is only available on older versions. In newer versions, it looks like you need to override Spree::Image. This is defined in vendor/bundle/gems/spree_core-2.2.0/app/models/spree/image.rb.
来源:https://stackoverflow.com/questions/22416990/paperclip-unable-to-change-default-path