【问题标题】:Paperclip unable to change default path回形针无法更改默认路径
【发布时间】:2023-07-06 05:54:02
【问题描述】:

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

我正在使用 Spree 2.2,它使用回形针 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

【问题讨论】:

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


    【解决方案1】:

    对于 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)。

    【讨论】:

      【解决方案2】:

      我相信 /home/username/path/to/project/ 部分是由您的 :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",
      

      让我知道这些对您有什么作用,我们可以了解您还需要做什么才能使其正常工作。

      【讨论】:

      • 其实问题是它似乎没有使用路径和url配置选项。我可以把任何东西放在那里,没有任何变化。我无法在我的配置中找到路径和 url 可能被覆盖的其他任何地方。
      • 你是对的,我没有意识到你的路径不包含名为 spree 的文件夹,但生成的 url 包含。很抱歉,我的第二个回答应该对你有更多帮助。
      【解决方案3】:

      在谷歌快速搜索后,根据您的评论提示,我明白了。我相信答案就在这里,在 Spree 文档中:

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

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

      【讨论】:

      • 不幸的是,我认为该文档已经过时,不适用于 Spree 2.2。他们删除了内置的 S3 集成,现在只能通过 Paperclip 完成。 Spree 2.2 的默认安装甚至没有图像配置页面。
      • 嗯,这是我能想到的唯一会覆盖你的回形针配置的地方。您粘贴的 URL 也与该文档显示的默认配置相匹配。抱歉,我帮不上忙,也许他们的支持团队可以帮助您。
      • 是的,我在 github 上开了一张票:github.com/spree/spree/issues/4448。感谢您的尝试!
      • 我知道这是在哪里定义的。看起来您需要覆盖 Spree::Image。这在 vendor/bundle/gems/spree_core-2.2.0/app/models/spree/image.rb 中定义。
      • 是的,我刚刚找到了,谢谢!它使用如下设置工作:github.com/spree/spree/issues/4448#issuecomment-37734007。但我不知道这是否是一个错误。等着看 Spree 开发者怎么说。