Forum: Ruby on Rails strange File.open and Dir.mkdir issue

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.
PeteSalty (Guest)
on 2007-01-25 11:21
(Received via mailing list)
I'm devloping an application that allows users to attach files to their
profiles. At the moment they can only attch one file per profile. We
have some unique requirements so we have to write our own code for this
(can't use act_as_attachment or file_column).

Anyhoo, we've got pretty much everything working except one really
bizzare problem. We want to save the attachments into a directory
structure like this

    public/attachments/<user_id>/<profile_id>

which would end up being something like

    public/attachments/23/18

the only problem is that when we try to either open the file for
writting with something like

      File.open(@save_location + '/' + @file_name, "wb") do |f|
        f.write(@temp_file.read)
      end

we get

   No such file or directory - public/attachments/23/18/test-file.jpg

or try and make a new directory using

    Dir.mkdir(@save_location) unless File.directory?(@save_location)

we get the folowing error:

   No such file or directory - public/attachments/23/18

or something similar depending on the user_id and profile_id of course.
This is happening on my development machine and I have to admint I
haven't tried it on another (don't have another set up or available at
this time) but I can't seem to work out why it won't create the
directories. If I try it with just say

      File.open('public/attachments/' + @file_name, "wb") do |f|
        f.write(@temp_file.read)
      end

it works fine however in this case both 'public' and 'attachments'
already exist so I'm guessing it's got something to do with creating
the new directories.

Any ideas? Could it have something to do with  the public directory
being under Subversion (although the attachments directory is not).

Thanks in advance
Dale
Chris M. (Guest)
on 2007-01-25 11:34
(Received via mailing list)
On Jan 25, 9:20 am, "PeteSalty" <removed_email_address@domain.invalid> wrote:
> We want to save the attachments into a directory
> structure like this
>
>     public/attachments/<user_id>/<profile_id>
>
> which would end up being something like
>
>     public/attachments/23/18

Are you trying to create the directory structure 23/18/ in one fell
swoop? Because the standard Dir.mkdir will only make a single directory
at a time.

If you want to create all of the directories necessary for your path in
one go, you can use FileUtils.mkdir_p:

http://www.ruby-doc.org/stdlib/libdoc/fileutils/rd...

which is equivalent to the 'mkdir -p' command at the shell.

Chris
PeteSalty (Guest)
on 2007-01-25 22:31
(Received via mailing list)
Chris,

Thank you so much for pointing this out, that certainly saved the day.

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