Paperclip unable to change default path

我们两清 提交于 2019-12-23 22:33:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!