Is there something like PHP's $_POST superglobal array available?

I'm trying to create file uploads to the file system and process them with RMagick, but the struggle is that I don't want to create dozens of tables or use plugins if I can avoid it. I want to keep it super simple.

So, is there some accessible instance variable equivalent to PHP's $_POST superglobal array? I'm getting the feeling that doing this is much easier in PHP, thought the code of actually processing files is much easier in Ruby.

Suggestions? Advice?

There's no superglobal variable for it, but controller actions can access params through the params method. params[:uploaded_file]. It contains all routing, GET, and POST variables.

Wouldn't

request.query_string

give you most of what you want?

Ok, that's interesting! Any code tips on how to go about using either : params[:uploaded_file]

or: request:query_string

?

I'm at a bit of a loss here. My Ruby is fine, but I'm still getting a hang of when and how things happen with Rails' controllers.

Basically, I'm trying to upload an image along with user-entered information about it. All I want is to save the associated info and the file's name and path to the DB. But I want to upload the file in the same form. I'd like to validate and process the file it before saving the info to the DB. I can do the Ruby for validating the file and processing it, but I'm lost on the Rails-specific stuff.

Maybe the plugin approach is going to be better in the short-term? (and rebuild everything around the plugin?) Or is it possible to access this params array and get all form data and do with it as I please within one controller method? (of course calling other methods within it)

Does this answer any questions?

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

Kind of answers some questions, but not very clearly. Honestly, seems like every tutorial or blog post is kind of incomplete in a sense for beginners doing uploads.

I guess I'll start from scratch and build around one of the upload plugins.

Strange...! I think your replies helped, but so did some of the stuff at this post: http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/b0805c5ebbbfd3e/86b40fc82b739bfd?lnk=gst&q=how+to+upload+files&rnum=7#86b40fc82b739bfd

Your post made it a little more clear what was going on at that post. I'm still not completely sure how this is working, because I had to add an empty def in the model def file_upload=(temp_file) end

I'm not even sure if it did anything. My code is a little messy now, from hacking through it. I'm going to have to spend some time cleaning it up, piece at a time to figure out what is and isn't needed in there.

I did call the methods to save my upload before the method to save my info to the db. So I guess I need to do the same when I render the thumbnails and pass their info to the db. If you'd like to see my now-ugly code to give me advice on it, I will gladly post it or e-mail a file !

Cheers John Joyce

You can always put a pastie up for people to look at.

http://pastie.caboo.se

Ok, here's what I've got so far, but now my RMagick is not working. Rails gives this error: unable to open image `public/images/image.jpg': No such file or directory

app/controllers/asset_controller.rb:108:in `write' app/controllers/asset_controller.rb:108:in `make_thumbnail' app/controllers/asset_controller.rb:78:in `create'

why not just save yourself a lot of pain and use a plugin such as attachment_fu.. I understand your reluctance to want to add a bunch of unnecessary tables, but really all you need is a single table for your attachments (which you'll probably want regardless of the method that you choose for uploading files) and attachment_fu can handle your validations, renaming of files, image resizing, etc.. Then you can focus on the parts of your application that really matter, instead of getting bogged down reinventing the wheel. (I speak from experience on this; I first created my own file uploading processor before there were any file upload plugins available, and I've since switched to using attachment_fu on all my projects)

Mike

Unless I'm misunderstanding, you're not giving Magick a filesystem
path. You're giving it a path relative to DocumentRoot. To open a
file with Magick, you have to provide a full path to the file in the
operating system's filesystem.

I hear you loud and clear. But I'm really interested in finding out how it works and making it work, before using a plugin. I almost went to using a plugin yesterday, then I got the upload working.

s.ross, my gratitude! That makes sense sort of. I wonder why the upload works with RAILS_ROOT then? Anyway, you are the king of kindness! You saved the day. I got it working. It's ugly. Needs serious refactoring and plastic surgery. It even borders on Comodore 64 BASIC full of GOTO type of spaghetti, but it's working!

So I say nay to all the "just use a plugin" nay-sayers! With people like you around, there's still hope for people like me who want to figure out how things are really working, rather than just cut and paste.

RAILS_ROOT gives you the filesystem path to the root of your Rails
application. From there you can navigate your entire directory tree.

The caveat about using a plugin is: Don't use one that you couldn't
write yourself. Even if you don't want to take the time, you should
have the requisite skill to understand what the plugin code is doing.
That said, attachment_fu is way cool, and clever beyond imagination.

-s

exactly why I don't want to use plugins yet! (short of ruby gems like RMagick, I'm not about to even try to write a big wrapper library like that)

Interestingly, I found that Rails in many places in my code complained about RAILS_ROOT, so perhaps it's not accessible in certain scopes? Which would be odd, so my fixes ended up being using File.expand_path and File.basename respectively assigned to instance variables in order to be sure I got the absolute path as you had suggested, and it's still portable too.

I'm still feeling a little bad using instance variables so much in Rails, but it keeps things visible that I need visible, and it's a lot easier (initially at least) than passing data from method to method. My style tends to just build methods that make the overall logic readable like pseudo-code. Still, my abundant use of instance variables makes me worried, because it feels like the old GOTO taboo of many languages.

well, if don't want to use plugins and this is just a learning exercise, why don't you pull apart attachment_fu and see what it does. It's well tested and well written, and would serve as a good starting point to writing your own file uploader.

Adam

You'll have to be more specific about where RAILS_ROOT is not
visible. It should be available where you need it. What are the error
messages and what did your code look like? Also, will you please
change the subject on this thread? It's morphed.