Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3, Paperclip - Custom Interpolations

I've been having some troubles making custom Interpolation, gone through every example I could find on web, but no matter what I did, had no success. At the moment I have this:

Model

has_attached_file :photo,
  :path => ":rails_root/public/images/:img_name-:style.:extension",
  :styles => {
    :original => '100x100',
    :thumb => '30x30'
}

initializers/paperclip.rb

Paperclip.interpolates :img_name do |attachment, style|
  attachment.instance.img_name
end

img_name is field populated in form on upload with the image. The error I get on upload is:

Invalid argument - (C:/Users/.../stream20110410-384-stl2lk20110230-213-1fm2bab, C:/.../photo_upload/public/images/:img_name-original.jpg)

like image 755
uhandoh Avatar asked Apr 10 '11 17:04

uhandoh


1 Answers

Seems to work if it's directly in the model:

class Model < ActiveRecord::Base

  Paperclip.interpolates :img_name do |attachment, style|
    attachment.instance.img_name
  end

  has_attached_file :photo,
    :path => ":rails_root/public/images/:img_name-:style.:extension",
    :styles => {
      :original => '100x100',
      :thumb => '30x30'
    }

end
like image 179
Jakub Hampl Avatar answered Jan 01 '23 20:01

Jakub Hampl