The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Syntax Questions (relative newbie)

Hey, I am working on Rails App and I am a bit wobbly on my syntax. I would like to send one of two kinds of send_file statements to a user depending on which kind of downloadable they ordered. I have basically just expanded on the depot code in the Agile Dev book.

For your main question, I would extract most of the logic to the model and I wouldn't bother storing the uploaded files in separate folders according to type because it makes things simpler and it makes little difference - the file can be stored anywhere and named anything (no file extension needed either) as long as you have a record of the content type in your database - this can be saved when its originally uploaded.

In the following example, attachments are stored in /var/www/attachments and they are saved using the format attachment_ID

One gotcha, even though the right content type will be sent for the download, it will simply be named attachment_ID without a file extension. Don't forget you can send a custom name for the file with send_file() so you may want to do that to give the download a more descriptive name (perhaps using some existing metadata you have about the file in the database, or perhaps the original filename if you store that in the db when its originally uploaded).

Luke, thanks a lot for your help. I am running into problems with the code's structure. I am a novice and trying to get this up and running without too much restructuring of the code/database (since I am so shaky and don't want to dig a deeper hole for myself). That being said is there no way (ruthlessly efficient or not) of keeping the logic basically as is, but having those last two options there at the end of the same controller? You are right about the keeping the files in the same file paths, though, and I will definitely do it that way.

I'd still advise looking into refactoring things to a better solution though. As long as your code is backed up by unit tests, and with the help of migrations for your db changes, it shouldn't be too difficult.

Thanks, a billion, Luke. I will give this a shot. I do think, though, that I will restructure the app in the future, but in the meantime I want to put this out into the world in a functioning state while I learn up on my Ruby knowledge. Thanks.