سؤال

Here is how the image is defined in the model:

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

When I get the url of an image by entering in this:

Image.find(143).image

it returns this:

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

The problem is that :product_id should return as 165 not as :product_id

When I get the product_id of an image by entering this:

Image.find(143).product_id

it returns 165

So I'm not sure what the issue is.

هل كانت مفيدة؟

المحلول

:product_id is treated as plain string and not evaluated by Paperclip because it is not in the list of Interpolation methods provided by Paperclip.

See all the methods provided by Paperclip which are available for Interpolation in path and url options: Module: Paperclip::Interpolations Documentation

Now, in order to create your own interpolation method you will have to do what Paperclip suggests in its Module: Paperclip::Interpolations documentation:

To add your own (or override an existing one), you can either open this module and define it, or call the Paperclip.interpolates method.

For example:

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top