-1

I am using this code for uploading the profile image of a user

if params[:user][:image].present?
  uploaded_io = params[:user][:image]
  name = "image_" << @user.username << uploaded_io.original_filename
  File.open(Rails.root.join('public', 'images','profile',name ), 'w') do |file|
    file.write(uploaded_io.read)
  end
end  

but its giving this error when i run it cannot convert ascii-8bit to utf-8bit so what i did was i changed my default encoding in application.rb file like this config.encoding = "ascii-8bit" from config.encoding = "utf-8". and it started working fine.

But the problem is now I am sending a mail to the user for successful registeration from simple rails mailer and now before sending the mail its giving me this error cannot convert utf-8bit to ascii-8bit.

Now I am totally struck only one of them works at one time. I have tried a lot of suggested solutions to enforce_encoding on strings but none of them seems to work. Any help would be appreciated. Thanks.

1
  • @ajt yeah the solutions suggest to change the mysql to mysql2 but i don have any error while saving in database its while writing to the image file. so I am still struck. thanks for the link btw. Commented Aug 14, 2013 at 6:35

1 Answer 1

1

changed 'w' to 'wb'

if params[:user][:image].present?
  uploaded_io = params[:user][:image]
  name = "image_" << @user.username << uploaded_io.original_filename
  File.open(Rails.root.join('public', 'images','profile',name ), 'wb') do |file|
    file.write(uploaded_io.read)
  end
end  
Sign up to request clarification or add additional context in comments.

1 Comment

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.