Forum: Ruby on Rails non ascii encoding

Posted by Jim ruther Nill (jimboker)
on 2012-11-13 08:24
(Received via mailing list)
Hi guys!

I have a company model with a name attribute.  Each company is 
association
a set of projects.  I created a feature which allows users to
download a csv version of these projects.  The download will give you a
zipped file of all projects grouped by company.  So the zip file 
contains x
number of csv files where x is the number of companies.  Everything is
working nice.

But when a user created a company whose name contains greek
characters, I suddenly get issues with encoding.  Here's a small part of
the code that creates the zip file (i'm using zipruby btw)

Zip::Archive.open(file, Zip::CREATE) do |archive|
  companies.each do |company|
    data = CSV.generate(row_sep: "\r\n", quote_char: '"', force_quotes:
true) do |csv|
      company.projects.each { |project| csv << project.name }
    end

    archive.add_buffer "#{company.name}.csv", data
  end
end

so when company.name contains greek characters, i get a filename similar 
to
company??????.csv which cant be opened.  I can't find a
way to set the encoding of the filename so I just gsubbed it and ignored
all non-ascii characters which is fine.  But if one of the projects also
has a greek character, then the csv file will contain weird non-greek
characters.

Any help or insight is appreciated. Thanks!


--
Posted by Max Shytikov (Guest)
on 2012-11-14 16:23
(Received via mailing list)
Hi Jim,

Looks that you have encoding problems, check your current encoding

puts Encoding.default_external

To solve your problem it should be UTF-8. Because according to the docs
http://www.ruby-doc.org/core-1.9.3/Encoding.html#m...

*"File <http://www.ruby-doc.org/core-1.9.3/File.html> data written to 
disk
will be transcoded to the default external encoding when written."*
*
*
Or you can use additional options :encoding in CSV.generate
(http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/C...)
Posted by Jim ruther Nill (jimboker)
on 2012-11-14 16:33
(Received via mailing list)
On Wed, Nov 14, 2012 at 11:21 PM, Max Shytikov <mshytikov@gmail.com> 
wrote:

> Hi Jim,


Hi Max, thanks for replying.  I gave


> Looks that you have encoding problems, check your current encoding
>
> puts Encoding.default_external
>

> To solve your problem it should be UTF-8. Because according to the docs
> http://www.ruby-doc.org/core-1.9.3/Encoding.html#m...
>
> *"File <http://www.ruby-doc.org/core-1.9.3/File.html> data written to
> disk will be transcoded to the default external encoding when written."*
>

I checked that out, it is set to utf-8

>> Encoding.default_external
=> #<Encoding:UTF-8>


> Or you can use additional options :encoding in CSV.generate (
> http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/C...
> )
>

i'll try this one out but it seems like if i pass the encoding here, all
strings passed will be processed as having that encoding right?
But anyway, it's the only lead I have so I'll give it a try. Thanks!


>> x
>> true) do |csv|
>> all non-ascii characters which is fine.  But if one of the projects also
>  --
>
--
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.