【问题标题】:Why isn't the url of my paperclip image working?为什么我的回形针图像的 url 不起作用?
【发布时间】:2014-04-05 01:41:54
【问题描述】:

这是图像在模型中的定义方式:

class Image < ActiveRecord::Base
  has_attached_file :image, :url => "/system/images/:product_id/:id/:style/:filename",
                    :styles => { :dashboard => '65x65>',
                                 :thumbnail => '174X174>',
                                 :large => '470' },
                    :default_url => ActionController::Base.helpers.asset_path('/assets/missing_:style.png')
end

当我通过输入以下内容获取图像的网址时:

Image.find(143).image

它返回这个:

/system/images/:product_id/143/original/logo11w.png?1383743837

问题是:product_id 应该返回为165 而不是:product_id

当我通过输入以下内容获得图像的product_id 时:

Image.find(143).product_id

它返回165

所以我不确定是什么问题。

【问题讨论】:

  • 您是否使用“脚本/生成回形针图像图像”命令生成图像模型?这个 product_id 是从哪里来的?
  • @Snubber 我的回答解决了您的问题吗?同上我为你回答的另一个问题。请告诉我。

标签: ruby-on-rails ruby paperclip


【解决方案1】:

:product_id 被视为纯字符串,不会由Paperclip 评估,因为它不在Paperclip 提供的插值方法列表中

pathurl 选项中查看Paperclip 提供的所有可用于插值的方法:Module: Paperclip::Interpolations Documentation

现在,为了创建您自己的 interpolation method,您必须按照 Paperclip 在其模块:Paperclip::Interpolations 文档中建议的操作:

添加您自己的(或覆盖 现有的),你可以打开这个模块并定义它,或者调用 Paperclip.interpolates 方法。

例如:

class Image < ActiveRecord::Base
  Paperclip.interpolates :product_id do |attachment, style|
    attachment.instance.product_id
  end
  has_attached_file :image, :url => "/system/images/:product_id/:id/:style/:filename",
                    :styles => { :dashboard => '65x65>',
                                 :thumbnail => '174X174>',
                                 :large => '470' },
                    :default_url => ActionController::Base.helpers.asset_path('/assets/missing_:style.png')
end

【讨论】:

  • @AlokAnand 我认为,她应该写一本书.. 嗯.. 至少博客.. 我说的对吗? ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
  • 2019-02-22
  • 1970-01-01
  • 2016-10-30
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多