Professional Web Applications Themes

another silly cgi question - Ruby

Hello Ruby cgi geniuses, I cannot figure out how to print html content within the same scope as the cgi.xx object. That is: When I try and run this: cgi.out() do cgi.html() do cgi.body() do p userinfo.to_s cgi.form("get") do cgi.br + cgi.params.map {|key, val| key + " " + cgi.text_field(key, val) + cgi.br }.join + cgi.br + cgi.submit("Does this look ok?") end end end end I get all the html written before the Content-Type and Content-length headers get printed...which of course kills my script. If I move the 'p userinfo.to_s' to after the cgi.submit then the script prints all of ...

  1. #1

    Default another silly cgi question

    Hello Ruby cgi geniuses,

    I cannot figure out how to print html content within the same scope as
    the cgi.xx object. That is: When I try and run this:


    cgi.out() do
    cgi.html() do
    cgi.body() do
    p userinfo.to_s
    cgi.form("get") do

    cgi.br +
    cgi.params.map {|key, val|
    key + " " +
    cgi.text_field(key, val) +
    cgi.br
    }.join +
    cgi.br +
    cgi.submit("Does this look ok?")
    end

    end
    end
    end

    I get all the html written before the Content-Type and Content-length
    headers get printed...which of course kills my script. If I move the 'p
    userinfo.to_s' to after the cgi.submit then the script prints all of
    cgi.xx html along with the userinfo data above the Content-Type:
    headers. Does anyone know how to shim your own html into the cgi object
    so that it will run correctly?

    Thanks,
    Cere
    Cere Guest

  2. #2

    Default Re: another silly cgi question

    Cere Davis <cereu.washington.edu> writes: 

    ^^^^^^^^^^^^^^^

    That's your problem. I'm not sure what 'userinfo' is here, but the
    pickaxe says about Kernel#p:

    For each object, directly writes anObject.inspect followed by the
    current output record separator to the program's standard output. p
    bypasses the Ruby I/O libraries

    That means that as soon as you call 'p userinfo.to_s', that gets
    printed immediately, instead of as part of your CGI output. This Is
    Bad(tm).

    The block you pass to CGI#body should return a string, which will be
    the contents of the '<body>' '</body>' tags. Instead, you're printing
    something to stdout, then building your string below, and after the
    string is built, the cgi routines spit out their HTML elements, with
    your string inside it.

    Also, your indentation is inconsistent and too hard to read at the
    start for my taste, and you don't need the () if you're not passing
    parameters to the HTML-generating methods. I'd prefer something that
    looked like:

    cgi.out { # or 'do'..'end', whatever you like
    cgi.html {
    cgi.body {
    userinfo.to_s +
    cgi.form('get') { # I only use "" when I'm interpolating variables
    cgi.params.collect { |key,val|
    key + ' ' + # HTML collapses spaces, so no need to put
    cgi.text_field(key,val) + # multiple ones here.
    cgi.br
    }.join("\n") + # I like "\n" here, as it makes the generated
    cgi.br + # HTML easier to read.
    cgi.submit("How's this?")
    }
    }
    }
    }
     

    I realize that at this very instant, Ruby makes that harder than
    necessary, but "Programming Ruby" is a decent reference, and is (most
    importantly) free. If you check out the doentation there, you'll
    see they provide a better and more complete explanation of cgi.rb than
    I have here.

    Thanks to Dave Thomas and many others, 'ri' will soon become the
    standard reference for Ruby doentation. I can't wait for the day
    the default response to any 'what is wrong with this code?' query
    becomes, "Did you read the doentation on your hard drive?"

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Guest

  3. #3

    Default Re: another silly cgi question

    Thanks for the critique on my coding style and yes I read the "lack of"
    doentation on my hard drive. It's pretty sp.

     
    Cere Guest

Similar Threads

  1. Silly question
    By Jim Macklin in forum Macromedia Flash Flashcom
    Replies: 7
    Last Post: February 22nd, 06:36 PM
  2. silly question...
    By Wendell in forum ASP.NET Web Services
    Replies: 2
    Last Post: September 16th, 02:24 PM
  3. D70 Silly Question
    By Himm in forum Photography
    Replies: 11
    Last Post: January 22nd, 05:39 AM
  4. Silly question from the UK
    By garryac in forum Photography
    Replies: 15
    Last Post: July 20th, 05:41 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139