1

I'm getting an error when attempting to change the :filename of my paperclip attachment to equal an attribute on the class I'm attaching the paperclip file to.

When I use "#{self.company_name}" it errors out. Apparently in this scope, "self" is not Company. When I wrote this line I assumed that self is the instance of Company that I'm uploading this attachment to. Any idea how I can fix this? The Paperclip docs say to use ":filename" but I'd like to use the value of Company.company_name instead.

class Company < ActiveRecord::Base
  include AliasAttrs

  has_attached_file :company_logo, {

      :storage => :ftp,

      :path => "/logos/#{self.company_name}",

      :url => FTP_CONFIG[:access_host]+"logos/:filename",

      :ftp_servers => [
        {
          :host     => FTP_CONFIG[:host],
          :user     => FTP_CONFIG[:user],
          :password => FTP_CONFIG[:pass],
          :port     => 21 # optional, 21 by default
        }
      ]
  }
end

Update

I tried using the advice found in this post: https://robots.thoughtbot.com/paperclip-tips-and-updates

But now I am getting the following error when starting my server:

undefined method `interpolations' for Paperclip::Attachment:Class (NoMethodError)

1 Answer 1

2

It looks like the syntax for interpolations has changed. Updated it and it worked. Add the following to your model or create a paperclip.rb file in config/initializers

  Paperclip.interpolates :company_name do |attachment, style|
    attachment.instance.company_name
  end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.