Forum: Ruby on Rails Problems with File_column

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
32b703cdb0ea03c3939b97a93205414f?d=identicon&s=25 John T. (johnt519)
on 2006-01-22 21:22
Hi,

Rails - newbie here..

I'm setting up an app and am trying to use file_column to upload a
picture with an item and I'm running into several problems.

(Background: I'm running Locomotive Rails under OS X 10.4.4, and using
Safari)

I followed the directions to add the necessary items, then found out
that I had to add the item to make the form tag be a multipart item.
(Sebastian - you may want to update your docs for this!)

However, when I changed the start_form_tag item to be:

<%= start_form_tag :action => 'create', :multipart => true %>

I get a form tag like:

<form action="/teams/create?multipart=true" method="post">

which leads to an error. I can change it to:

<%= start_form_tag({:action => 'create'}, :multipart => true) %>

which generates the correct form tag. Why does the first one not work?

Then, when I tried to submit, Safari would just time out. I found an
item about changing the lighttp.conf to work with Safari, but that
didn't help.

If I then tried Firefox, I would then get an error instead:

same file:
/Users/john/Desktop/Mytest/public/../config/../public/team/image/tmp/1137961236.548784.3649/IMG_0008.JPG
and
/Users/john/Desktop/Mytest/public/../config/../public/team/image/tmp/1137961236.548784.3649/IMG_0008.jpg

I did some digging and found out that if the original file is in
UPPERCASE, then that error results. If I choose a file that has
lowercase, the file uploads fine in both Firefox and Safari. (In
Firefox, you can edit and change the filename to be lowercase and it
will work).

Is there any way around this problem?!?

Thanks!
32b703cdb0ea03c3939b97a93205414f?d=identicon&s=25 John T. (johnt519)
on 2006-01-22 21:30
John Tsombakos wrote:
> Hi,
>
> Rails - newbie here..
>

One more quick question...

On my Edit form, (which is using the same form as for the "new" item),
the field for the image is showing "no image selected" (which sort of
makes sense). Can I just show the existing file name somehow? Or image?

Also, I had to change the the start_form_tag for my edit page from:

<%= start_form_tag :action => 'update', :id => @team, :multipart => true
%>

To:

<%= start_form_tag({:action => 'update', :id => @team}, :multipart =>
true) %>

to get the proper <form> tag. And through some trial and error I found I
had to put the braces where they are (which I'm sure is just Ruby syntax
that I'm missing...)

Thanks again.
7e5be8d0d2537a6f6f00aada5541881e?d=identicon&s=25 Steve Ross (Guest)
on 2006-01-22 21:44
(Received via mailing list)
Not a stupid question. If you are running lighttpd, you have to modify
it to
add the following to your lighttpd.conf:

# for safari file uploads
$HTTP["useragent"] =~ "^(.*MSIE.*)|(.*AppleWebKit.*)$" {
    server.max-keep-alive-requests = 0
  }

If you are running Locomotive, open

/Applications/Locomotive/Bundles/lighttpd<something_or_oter>.bundle/Contents
/Resources/base_lighttpd.conf

And make the change there. That should get you going. Oh, and don't
forget
to bounce the server :)
4bd34a2216dc8bdbf1f017f64e4d59e8?d=identicon&s=25 Kyle Maxwell (Guest)
on 2006-01-22 21:47
(Received via mailing list)
On 1/22/06, John Tsombakos <johnts@charter.net> wrote:
> makes sense). Can I just show the existing file name somehow? Or image?
>
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


The reason for the curly braces is that the method takes two hashes as
arguments.  Without the curly braces, Ruby assumes that you intended
the last key/value pair to be a part of the first hash, when in
reality, its part of the second.

Also, file_column_field doesn't tell you whats currently there, but
you could always show it manually like:

<%=link_to "current contents", url_for_file_column("model", "field")
if @model.field %>

--
Kyle Maxwell
Chief Technologist
E Factor Media // FN Interactive
kyle@efactormedia.com
1-866-263-3261
32b703cdb0ea03c3939b97a93205414f?d=identicon&s=25 John T. (johnt519)
on 2006-01-22 21:53
Steve Ross wrote:
> Not a stupid question. If you are running lighttpd, you have to modify
> it to
> add the following to your lighttpd.conf:
>
> # for safari file uploads
> $HTTP["useragent"] =~ "^(.*MSIE.*)|(.*AppleWebKit.*)$" {
>     server.max-keep-alive-requests = 0
>   }
>
> If you are running Locomotive, open
>
> /Applications/Locomotive/Bundles/lighttpd<something_or_oter>.bundle/Contents
> /Resources/base_lighttpd.conf
>
> And make the change there. That should get you going. Oh, and don't
> forget
> to bounce the server :)

Ah.. thanks for that! I added that line into the wrong place - in:

Locomotive/Bundles/rails-1.0.0-min.bundle/Contents/Resources/ports/lib/ruby/gems/1.8/gems/rails-1.0.0/configs
32b703cdb0ea03c3939b97a93205414f?d=identicon&s=25 John T. (johnt519)
on 2006-01-22 21:55
Steve Ross wrote:
> Not a stupid question. If you are running lighttpd, you have to modify
> it to
> add the following to your lighttpd.conf:
>
> # for safari file uploads
> $HTTP["useragent"] =~ "^(.*MSIE.*)|(.*AppleWebKit.*)$" {
>     server.max-keep-alive-requests = 0
>   }
>
> If you are running Locomotive, open
>
> /Applications/Locomotive/Bundles/lighttpd<something_or_oter>.bundle/Contents
> /Resources/base_lighttpd.conf
>
> And make the change there. That should get you going. Oh, and don't
> forget
> to bounce the server :)

Hmm. Unfortunately, that does fix the error:

same file:
/Users/john/Desktop/Mytest/public/../config/../public/team/image/tmp/1137963285.522411.3748/IMG_0202.JPG
and
/Users/john/Desktop/Mytest/public/../config/../public/team/image/tmp/1137963285.522411.3748/IMG_0202.jpg

which deals with the uppercase filename/extension..
32b703cdb0ea03c3939b97a93205414f?d=identicon&s=25 John T. (johnt519)
on 2006-01-22 21:56
John Tsombakos wrote:
>
> Hmm. Unfortunately, that does fix the error:
>

Errr... Make that does _not_ fix the error....
7e5be8d0d2537a6f6f00aada5541881e?d=identicon&s=25 Steve Ross (Guest)
on 2006-01-22 22:06
(Received via mailing list)
A couple of references that may or may not fix your problem:

http://wiki.rubyonrails.com/rails/pages/HowToUseFileColumn

http://wiki.rubyonrails.com/rails/pages/UploadingF...

Search the forum ( http://www.ruby-forum.com/forum/3) -- I seem to
recall a
few issues like this before.

Also, look at the source to see where the case translation is taking
place
on the extension. That may provide a clue to what the problem is and
suggest
a fix.
32b703cdb0ea03c3939b97a93205414f?d=identicon&s=25 John T. (johnt519)
on 2006-01-22 22:13
Steve Ross wrote:
> A couple of references that may or may not fix your problem:
>
> http://wiki.rubyonrails.com/rails/pages/HowToUseFileColumn
>
> http://wiki.rubyonrails.com/rails/pages/UploadingF...
>
> Search the forum ( http://www.ruby-forum.com/forum/3) -- I seem to
> recall a
> few issues like this before.
>
> Also, look at the source to see where the case translation is taking
> place
> on the extension. That may provide a clue to what the problem is and
> suggest
> a fix.

Thanks for those links, unfortunately there isn't anything there about
the case problem. I had done a search on the forum and did see one post
about the problem, but without a solution. I guess it's time to start
looking at the source :)

Thanks again.

jt
7e5be8d0d2537a6f6f00aada5541881e?d=identicon&s=25 Steve Ross (Guest)
on 2006-01-22 23:34
(Received via mailing list)
In file_column.rb, find this code:

    def store_upload(file)
      @tmp_dir = FileColumn.generate_temp_name
      @dir = File.join(tmp_base_dir, @tmp_dir)
      FileUtils.mkdir(@dir)

      @filename = FileColumn::sanitize_filename(file.original_filename)
      local_file_path = File.join(tmp_base_dir,@tmp_dir,@filename)


Prior to the @filename = stick:

RAILS_DEFAULT_LOGGER.debug "original filename =
#{file.original_filename}"

After the @filename = stick:

RAILS_DEFAULT_LOGGER.debug "sanitized filename = #{@filename}"

After the local_file_path = stick:

RAILS_DEFAULT_LOGGER.debug "Local file path = #{local_file_path}"

Let's see if the case translation is happening there. Sometimes a few
print
statements make a world of difference.
32b703cdb0ea03c3939b97a93205414f?d=identicon&s=25 John T. (johnt519)
on 2006-01-23 02:12
Steve Ross wrote:
> In file_column.rb, find this code:
...
> Let's see if the case translation is happening there. Sometimes a few
> print
> statements make a world of difference.

This is what I get:

original filename = IMG_0095.JPG
sanitized filename = IMG_0095.JPG
Local file path =
/Users/john/Desktop/Mytest/public/../config/../public/team/image/tmp/1137978200.835250.3798/IMG_0095.JPG


The error is being generated from this line in file_column.rb:

FileUtils.mv(local_file_path, new_local_file_path) unless
new_local_file_path == local_file_path

The only difference between the two paths is the case of the extension
(JPG vs jpg). I suppose I could change the test to compare like-cased
paths....

Yes, that did avoid the error in my case, but I suppose in the general
case, in other OS's where it handles case of files differently, it may
not be quite correct.

(an aside.. that sure is a convoluted path that is generated to the file
- up and back down and up and down into the same directory...)

Thanks!
F7d94c4c159ec28690c0190bbd304ec4?d=identicon&s=25 Courtenay (Guest)
on 2006-03-09 20:41
(Received via mailing list)
If anyone's still having this problem, I solved it and only just found
this thread, and thought I'd provide some actual code.  It's a
one-line fix.

Change
> FileUtils.mv(local_file_path, new_local_file_path) unless new_local_file_path == 
local_file_path

to

> FileUtils.mv(local_file_path, new_local_file_path) unless new_local_file_path.downcase 
== local_file_path.downcase


The code here is comparing the filename of the original file, and its
sanitized version. In some cases where there was an uppercase upload,
your case-(in)sensitive OS will spit an error.  This fix doesn't have
any negative effect, because we're just sanitizing filenames, nothing
else.

Good luck!

courtenay
http://blog.caboo.se
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-03-09 21:15
(Received via mailing list)
On 3/9/06, Courtenay <court3nay@gmail.com> wrote:
> If anyone's still having this problem, I solved it and only just found
> this thread, and thought I'd provide some actual code.  It's a
> one-line fix.
>

Hey! I recognize that fix! :)

Glad it helped..

jt
This topic is locked and can not be replied to.