2

I am using omniauth-facebook and trying to get the profile picture to show up using paperclip. Here is the code I use to get the picture to my User model

def picture_from_url(url)
    self.profile_pic =open(url)
end

However, it always saves as stringio.txt. So searching on this behavior I found out that paperclip allows for callbacks so I wrote the following method in the User model to rename the filename

def rename_profile_pic
    self.profile_pic.instance_write :filename, "#{self.username}.jpg"
end 

and passed it to the callback

before_post_process :rename_profile_pic

But this doesn't seem to help.

Any ideas how i can fix this ?

thanks

Rahul garg
  • 9,202
  • 5
  • 34
  • 67
Bindesh Vijayan
  • 161
  • 1
  • 4

3 Answers3

10

In case you haven't found the solution yet:

data = StringIO.new(file_data)
data.class.class_eval { attr_accessor :original_filename, :content_type }

data.content_type = content_type
data.original_filename = file_name

object.attachment = data
smcdrc
  • 1,671
  • 2
  • 21
  • 29
  • I got everything working nicely by combining this answer with [this one](http://stackoverflow.com/a/6325039/574190). – David Tuite Oct 17 '13 at 15:28
1

Convert your stringio.txt to file using this:

file = StringIO.new(obj)
file.original_filename = "#{self.username}.jpg"

and then assign your file to profile_pic

Rahul garg
  • 9,202
  • 5
  • 34
  • 67
  • Thanks, but that didn't help since open() was already giving a StringIO object and there isn't a method named original_filename on StringIO. Though I found the issue and fixed it. It is :file_name instead of :filename self.profile_pic.instance_write :file_name, "#{self.username}.jpg" – Bindesh Vijayan Dec 22 '12 at 17:02
0

My solution for creating files from a string:

class FileIO < StringIO
  def initialize(content:, filename:)
    super(content)
    @original_filename = filename
  end

  attr_reader :original_filename
end

FileIO.new(content: obj, filename: "#{username}.jpg")

This helped me solve the problem with the Carrierwave error when saving the file:

TypeError: no implicit conversion of nil into String

Viktor Ivliiev
  • 1,015
  • 4
  • 14
  • 21