Paperclip无法更改默认路径

时间:2014-03-14 22:34:47

标签: ruby-on-rails-4 paperclip spree

我遇到与this问题类似的问题,但我根本无法修改路径。

我正在使用Spree 2.2,它使用paperclip 3.4.2

我正在尝试修改回形针默认值以更改图像路径。所有其他配置都在运行。

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

但是使用此代码,URL就像这样:
https://s3-eu-west-1.amazonaws.com/bucketname/home/username/path/to/project/public/spree/products/24/original/ror_baseball.jpeg?1390110939

我已尝试将以下内容添加到该配置块中:

config.attachment_path = '/spree/products/:id/:style/:basename.:extension'

我也尝试将以下内容添加到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"

还尝试过:

Paperclip::Attachment.default_options.merge!(
  :path => ":rails_root/public/:attachment/:id/:style/:basename.:extension", 
  :url => "/:attachment/:id/:style/:basename.:extension"
)

有什么想法吗?

更新:opened ticket on github

3 个答案:

答案 0 :(得分:3)

对于Spree 2.2(不是2.1),请在initializers / spree.rb中使用以下代码:

Spree::Image.attachment_definitions[:attachment][:url] = ':path'
Spree::Image.attachment_definitions[:attachment][:path] = '/spree/products/:id/:style/:basename.:extension'

配置的其余部分应设置为2.1(有关详细信息,请参阅here。)

答案 1 :(得分:0)

我相信/ home / username / path / to / project / part是由:path配置的:rails_root部分添加的。

如果你看一下源代码: https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/attachment.rb

您将看到默认值如下:

:path => ":rails_root/public:url",
:url => "/system/:class/:attachment/:id_partition/:style/:filename",

如果您设置以下内容该怎么办:

:path => ":url",
:url => "/spree/products/:id/:style/:basename.:extension",

让我知道这些为您做了什么,我们可以看到您需要做些什么才能使其正常工作。

答案 2 :(得分:0)

经过快速Google搜索后,我发现你的评论提示我。我相信答案就在这里,在Spree文档中:

http://guides.spreecommerce.com/user/configuring_images.html

更新:显然这仅适用于旧版本。在较新的版本中,您似乎需要覆盖Spree :: Image。这在vendor / bundle / gems / spree_core-2.2.0 / app / models / spree / image.rb中定义。