Is there something like PHP's $_POST superglobal array avail

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?

On 7/22/07, [email protected]
[email protected] wrote:

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.


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

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.

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

http://pastie.caboo.se

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 J.

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:inmake_thumbnail’
app/controllers/asset_controller.rb:78:in `create’

Well, it IS still saving the upload, so the file IS there! But I can’t
seem to find the error in my code. Everything I try leads me back to
the same error. My logic is flawed. (no surprise)
So here’s what’s in my controller: (tell me if more code is more
helpful)

def file_upload(ul_file)
File::File.open “#{RAILS_ROOT}/public/images/
#{ul_file.original_filename}”,‘wb’ do |f|
f.write(ul_file.read)
@asset.file_name = ul_file.original_filename
@image_to_alter = “#{RAILS_ROOT}/public/images/
#{ul_file.original_filename}”
# make_thumbnail(ul_file)
# make_thumbnail(image_to_alter)
# make_comp(image_to_alter)
end
end

def save_file(ul_file)
File.open(ul_file, ‘wb’)
end

def create
@asset = Asset.new(params[:asset])
# ul_file = File.open(params[“asset”][“file_upload”], “w”)
file_upload(params[“asset”][“file_upload”])
make_thumbnail(@image_to_alter)
if @asset.save
flash[:notice] = ‘Asset saved.’
redirect_to :action => ‘list’
else
flash[:notice] = ‘error’
render :action => ‘new’
end
end

def destroy
Asset.find(params[:id]).destroy
redirect_to :action => ‘list’
end

def make_thumbnail(image_to_alter)
#rmagick code here

# Begin thumbing
img = Image.read(image_to_alter)[0]

thumbnail_height = 100
thumbnail_width = 100

geometry_obj = Geometry.new(thumbnail_width, thumbnail_height,

nil, nil, ‘!’)
chg_geom_img = img.change_geometry(geometry_obj) {|cols, rows,
image| image.resize(cols, rows)}
chg_geom_img_name = ‘thumbnail’ + img.filename
# chg_geom_img_name.gsub!(’-’, ‘’)
# chg_geom_img_name.gsub!(’ ', '
’)

if chg_geom_img.write(chg_geom_img_name)
  @asset.thumbnail.file_name = chg_geom_img_name
  @asset.thumbnail.file_size =  File.size?("#{RAILS_ROOT}/public/

images/#{chg_geom_img_name}")

  file_to_test = File.open("#{RAILS_ROOT}/public/images/

#{chg_geom_img_name}", ‘r’)

  # Test the types, let's hope this works
  case file_to_test
  when File.jpg?(file_to_test)
    my_file_type = 'jpg'
  when File.gif?(file_to_test)
    my_file_type = 'gif'
  when File.png?(file_to_test)
    my_file_type = 'png'
  end

  # Set the data
  @asset.thumbnail.file_type = my_file_type
  @asset.thumbnail.mime_type = 'image/' + my_file_type
end

end # make_thumbnail()

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

On 7/23/07, [email protected]

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.

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.

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.

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

On 7/23/07, [email protected]

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.