Professional Web Applications Themes

cgi params in ruby - Ruby

Hi Rubiers, I can't figure out a way to make the following code do what I want: #!/usr/bin/ruby1.8 -w require 'cgi' cgi=CGI.new("html3") cgi.out() do cgi.html() do cgi.body() do cgi.form("get") do cgi.params.to_a.each {|key, val| cgi.text_field(key, val.to_s) + cgi.br } + cgi.submit("Okey Dokey?") end end end end So say I post in something this when I run the script on the command line: (offline mode: enter name=value pairs on standard input) lname=davis;fname=cere ../t.cgi:15:in `+': cannot convert String into Array (TypeError) from ./t.cgi:15 from ./t.cgi:9:in `form' from ./t.cgi:9 from ./t.cgi:8:in `body' from ./t.cgi:8 from ./t.cgi:7:in `html' from /usr/lib/ruby/1.8/cgi.rb:1678:in `html' from /usr/lib/ruby/1.8/cgi.rb:1678:in `html' from ...

  1. #1

    Default cgi params in ruby

    Hi Rubiers,


    I can't figure out a way to make the following code do what I want:

    #!/usr/bin/ruby1.8 -w

    require 'cgi'

    cgi=CGI.new("html3")
    cgi.out() do
    cgi.html() do
    cgi.body() do
    cgi.form("get") do

    cgi.params.to_a.each {|key, val|
    cgi.text_field(key, val.to_s) +
    cgi.br
    } +
    cgi.submit("Okey Dokey?")

    end
    end
    end
    end

    So say I post in something this when I run the script on the command line:


    (offline mode: enter name=value pairs on standard input)
    lname=davis;fname=cere

    ../t.cgi:15:in `+': cannot convert String into Array (TypeError)
    from ./t.cgi:15
    from ./t.cgi:9:in `form'
    from ./t.cgi:9
    from ./t.cgi:8:in `body'
    from ./t.cgi:8
    from ./t.cgi:7:in `html'
    from /usr/lib/ruby/1.8/cgi.rb:1678:in `html'
    from /usr/lib/ruby/1.8/cgi.rb:1678:in `html'
    from ./t.cgi:7
    from ./t.cgi:6:in `out'
    from ./t.cgi:6


    What I want to happen is for the cgi script to print the lname and fname
    into two text_field boxes but I am having trouble tracing through this
    error. Can anyone help?

    Thanks,
    Cere
    Cere Guest

  2. #2

    Default Re: cgi params in ruby

     

    -> Array
     

    -> String.

    Array+String=Problem

    e.g. you could store the result from the .each in a string

    cgi.body() do
    s= ''
    cgi.form("get") do
    cgi.params.to_a.each {|key, val|
    s << cgi.text_field(key, val[0].to_s) + cgi.br
    }
    s+cgi.submit("Okey Dokey?")
    end
    end


    hope this is what u wanted,
    -Armin


    Armin Guest

  3. #3

    Default Re: cgi params in ruby


    On Feb 13, 2004, at 1:14 PM, Cere Davis wrote:
     

    interestingly enough, I was just working on that same problem this
    morning :)

    It looks like there is one error, and one unecessary complication.
    First, you don't need to convert the cgi.params to an array. You can
    use it as a hash just fine. Second, the CGI methods like #text_field
    don't print their result, they return them. So, since you are using
    Array#each, the value of the statement is... the array itself.
    Instead, you need to use #map and join the results:
     
    # use Hash#map(), which passes keys and values 
    Note that you get this error here because #each returns an array, and
    you are trying to add a string to it: 

    HTH,
    Mark



    Mark Guest

  4. #4

    Default Re: cgi params in ruby


    Thank you both for helping me with this.



    This an error I am doomed to repeat I'm afraid because I understand
    Hash.map and join systax (and didn't think it existed because ri didn't
    list it under the Hash method).

    Hash.map would map to an array of key val pairs I suppose...where val
    would be an array still. Then text fields get generated with <br>...
    Then when I .join it's doing what?

    Thanks.
     
    > # use Hash#map(), which passes keys and values
    > [/ref]
    Cere Guest

  5. #5

    Default Re: cgi params in ruby


    On Feb 13, 2004, at 3:20 PM, Cere Davis wrote:
     

    First of all, part of the problem you had, I think, is with the (IMHO)
    strange syntax that is used in the CGI module. When you call
    cgi.h1{"Title"}, or something like it, all it does is wrap it in the
    tag, so:

    cgi.h1{"Title"} #=> "<h1>Title</h1>"
    or:
    cgi.h1{"A " + cgi.bold{"Bold"} + " Title"} #=> "<h1>A <b>Bold</b>
    Title</h1>

    It just returns the string you gave it, but wrapped in a tag. So, if
    you do this:
    cgi.params.map{|key,value| cgi.text_field(key, val.to_s) + cgi.br }

    You end up getting an array of strings that each look something like
    this:
    '<input type="text" name="key" value="value"><br>'

    You finish it up by #joining the strings.
    cgi.params.map{|key,value| cgi.text_field(key, val.to_s) + cgi.br }.join

    Which gives you a string containing valid html markup, which can be
    passed (in a block) to cgi.form{}, etc.
    Also, calling aHash.map is functionally the same as calling
    aHash.to_a.map: it passes key/value pairs to the block.

    Cheers,
    Mark



    Mark Guest

  6. #6

    Default Re: cgi params in ruby


    I agree with you about the cgi syntax strangeness. If you want to hand
    enter tag syntax you have to someone splice it in the cgi object
    instance and this sort of seems like to heavy of a burned to put on the
    end user who just wants to write some simple code. Maybe it's just my
    ignorance but I've found this package to be pretty inflexible.

    related to that actually...

    I wanted to just print labels before the text_field statements so that
    one can see the title of the text field that they are filling in.

    My intuition would be to just do:

    cgi.params.map {|key, val|
    cgi.print key +
    cgi.print " :" +
    cgi.text_field(key, val.to_s) +
    cgi.br
    }.join + # and join the result before moving on
    cgi.submit("Okey Dokey?")

    But noooo. It fails. Sorry for being a doofus but what am I doing
    wrong now?


    Thanks for the explanation below BTW. I get it now.

     
    Cere Guest

  7. #7

    Default Re: cgi params in ruby


    On Feb 13, 2004, at 4:44 PM, Cere Davis wrote:
     

    here:

    # You want the following code block to return a string each time;
    # it'll map to an array of strings, which you join at the end into one
    string:
    cgi.params.map {|key, val|
    # cgi.print immediately outputs what you pass it, rather than
    # adding it to the string. just stick a string in there:
    key + " :" +
    cgi.text_field(key, val.to_s) +
    cgi.br
    }.join + # and join the result before moving on
    cgi.submit("Okey Dokey?")

    And that should do what you want. Everything is a string; the #title
    and #text_field are just shortcuts for creating the html tags. They
    always take strings and return strings.

    Cheers,
    Mark



    Mark Guest

Similar Threads

  1. Params in IE
    By Ralf Junge in forum Macromedia Flash Data Integration
    Replies: 0
    Last Post: February 19th, 09:45 AM
  2. cgi.rb params initialize_query()
    By Dan in forum Ruby
    Replies: 2
    Last Post: January 24th, 04:40 PM
  3. [ANN] ruby-freedb, ruby-serialport, ruby-mp3info moved to Rubyforge
    By guillaume.pierronnet@ratp.fr in forum Ruby
    Replies: 0
    Last Post: August 31st, 11:57 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