[Rails] Re: Re: Upload UTF-8 encoded textfile

2014-07-17 Thread Ronald Fischer
Colin Law wrote in post #1152686:
 On 17 July 2014 15:42, Eric Saupe ericsa...@gmail.com wrote:
 That shows how to create a Tempfile with a given encoding but the question
 is when a user uploads a file through a form and Rails creates a Tempfile is
 there a way to indicate that it should always create those Tempfiles with a
 default encoding such as UTF-8?

 In that case it *is* a Rails specific issue, not a Ruby question as
 suggested by the OP.

Indeed, you are right so far that it *might* be a Rails question. Still, 
I wonder why (in general) it is not possible to change the encoding of 
an existing (already open) Tempfile. Assuming that it is OK to rewind 
the file, I don't see a technical reason, why this is not possible.

I don't think it would be a good idea to configure this on the Rails 
side. Image the following scenario: We have a website, which allows 
users to upload textfiles, the content of which will eventually go into 
the database. Since we are generous about the encoding, we also provide 
the user with a dropdown list to choose a suitable encoding.

When the user clicks the upload button, the controller gets the uploaded 
file plus information about the encoding. Clearly, Rails can not 
anticipate the encoding of the file. It just can upload the file 
(binary), and provide the controller with an open file handle.

Now Ruby *does* have the set_encoding method for File, and Tempfile is-a 
file, and set_encoding *can* be called - it just fails. We have nearly 
everything in place. Now, if we can find out WHY set_encoding fails (and 
this might be a generic Ruby question), we can find out what Rails (or 
the programmer) can do to let things go smoothly

Ronald

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/4cf778a5a83ac07451bfc97d5fe0bb00%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] Re: Re: Upload UTF-8 encoded textfile

2014-07-17 Thread Eric Saupe
OK, so after some digging. It seems that when you create your new File 
object and set the encoding you may not need to read the Tempfile in its 
entirety. You can create a new File object using the Tempfile. 
File.new(my_temp_file, encoding: 'utf-8') and then use this file. It should 
be using the Tempfile and just creating a new pointer to that file with a 
new encoding. If you wanted to read the lines out individually and just use 
that original Tempfile you could use force_encoding('utf-8') on each line 
to make sure it is converting them to utf-8.

On Thursday, July 17, 2014 10:46:30 AM UTC-6, Ruby-Forum.com User wrote:

 Colin Law wrote in post #1152686: 
  On 17 July 2014 15:42, Eric Saupe eric...@gmail.com javascript: 
 wrote: 
  That shows how to create a Tempfile with a given encoding but the 
 question 
  is when a user uploads a file through a form and Rails creates a 
 Tempfile is 
  there a way to indicate that it should always create those Tempfiles 
 with a 
  default encoding such as UTF-8? 
  
  In that case it *is* a Rails specific issue, not a Ruby question as 
  suggested by the OP. 

 Indeed, you are right so far that it *might* be a Rails question. Still, 
 I wonder why (in general) it is not possible to change the encoding of 
 an existing (already open) Tempfile. Assuming that it is OK to rewind 
 the file, I don't see a technical reason, why this is not possible. 

 I don't think it would be a good idea to configure this on the Rails 
 side. Image the following scenario: We have a website, which allows 
 users to upload textfiles, the content of which will eventually go into 
 the database. Since we are generous about the encoding, we also provide 
 the user with a dropdown list to choose a suitable encoding. 

 When the user clicks the upload button, the controller gets the uploaded 
 file plus information about the encoding. Clearly, Rails can not 
 anticipate the encoding of the file. It just can upload the file 
 (binary), and provide the controller with an open file handle. 

 Now Ruby *does* have the set_encoding method for File, and Tempfile is-a 
 file, and set_encoding *can* be called - it just fails. We have nearly 
 everything in place. Now, if we can find out WHY set_encoding fails (and 
 this might be a generic Ruby question), we can find out what Rails (or 
 the programmer) can do to let things go smoothly 

 Ronald 

 -- 
 Posted via http://www.ruby-forum.com/. 


-- 
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/21394543-54bb-40b4-bb19-229300b9a4fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.