rails - Paperclip file name

AnApprentice picture AnApprentice · Nov 29, 2010 · Viewed 22.3k times · Source

using rails with Paperclip, I can use the following to get the filename during a before_create:

extension = File.extname(photo_file_name).downcase

How do I get JUST the file name.. Right now I have photo_file_name which provides the entire file, titlename.pdf

i need just titlename without the .pdf

Thanks

Updating with code:

photo.rb:

  before_create :obfuscate_file_name

  #Paperclip for photo
  has_attached_file :photo,
......


private

  def obfuscate_file_name
    extension = File.extname(photo_file_name).downcase
    fileNameOnly = File.basename(photo_file_name).downcase
    self.photo.instance_write(:file_name, "#{fileNameOnly}_#{ActiveSupport::SecureRandom.hex(32)}#{extension}")
  end

Answer

Jacob Relkin picture Jacob Relkin · Nov 29, 2010

Use File.basename with the optional suffix argument like this:

file_name = File.basename(photo_file_name, File.extname(photo_file_name));

Works on my machine:

alt text