Forum: Ruby on Rails Is there something like PHP's $_POST superglobal array avail

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 (Guest)
on 2007-07-23 01:16
(Received via mailing list)
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?
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-07-23 01:23
(Received via mailing list)
On 7/22/07,
<> 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 Olson
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2007-07-23 01:27
(Received via mailing list)


give you most of what you want?
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 (Guest)
on 2007-07-23 01:46
(Received via mailing list)
Ok, that's interesting!
Any code tips on how to go about using either :



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)
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2007-07-23 01:49
(Received via mailing list)
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 (Guest)
on 2007-07-23 02:53
(Received via mailing list)
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
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 (Guest)
on 2007-07-23 03:54
(Received via mailing list)
I think your replies helped, but so did some of the stuff at this

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)

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 !

John Joyce
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2007-07-23 05:11
(Received via mailing list)
You can always put a pastie up for people to look at.
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 (Guest)
on 2007-07-23 19:02
(Received via mailing list)
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

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'
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

  def file_upload(ul_file) "#{RAILS_ROOT}/public/images/
#{ul_file.original_filename}",'wb' do |f|
      @asset.file_name = ul_file.original_filename
      @image_to_alter = "#{RAILS_ROOT}/public/images/
      # make_thumbnail(ul_file)
      # make_thumbnail(image_to_alter)
      # make_comp(image_to_alter)

  def save_file(ul_file), 'wb')

  def create
    @asset =[:asset])
    # ul_file =["asset"]["file_upload"], "w")
      flash[:notice] = 'Asset saved.'
      redirect_to :action => 'list'
      flash[:notice] = 'error'
      render :action => 'new'

  def destroy
    redirect_to :action => 'list'

  def make_thumbnail(image_to_alter)
    #rmagick code here

    # Begin thumbing
    img =[0]

    thumbnail_height = 100
    thumbnail_width = 100

    geometry_obj =, 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/

      file_to_test ="#{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'

      # Set the data
      @asset.thumbnail.file_type = my_file_type
      @asset.thumbnail.mime_type = 'image/' + my_file_type
  end # make_thumbnail()
0900e6a4828bd989f96427082c6c74ca?d=identicon&s=25 Mike Garey (random52k)
on 2007-07-23 19:11
(Received via mailing list)
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)


On 7/23/07,
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2007-07-23 19:20
(Received via mailing list)
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.
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 (Guest)
on 2007-07-23 19:22
(Received via mailing list)
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.
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 (Guest)
on 2007-07-23 20:47
(Received via mailing list)
s.ross, my gratitude!
That makes sense sort of. I wonder why the upload works with
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.
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2007-07-23 20:52
(Received via mailing list)
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.

1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 (Guest)
on 2007-07-23 23:29
(Received via mailing list)
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.
581905e940c1e30190108a0c5c0b915b?d=identicon&s=25 Adam Cohen (Guest)
on 2007-07-24 00:19
(Received via mailing list)
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.


On 7/23/07,
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2007-07-24 03:11
(Received via mailing list)
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.
This topic is locked and can not be replied to.