Ruby on the web - a simple example using CGI
I've added a CGI example in Ruby - as part of the Ruby training I was doing yesterday. Source code is [here]
to show how it works / "Proof of concept". In practise, I would use the cgi module in Ruby if I was doing server side work using CGI - "Hello World" using that module is [here]
, or if the application was much more than trivial I would use a framework such as Ruby on Rails
Form inputs (using the default GET method) are delivered in the QUERY_STRING variable in the environment. They take the form of a series of & separated name=value pairs, which I separate out into a hash. The I have decoded the values further, as they may contain + characters for spaces, and % followed by two hex digits for other special characters. In theory, the names could be encoded in a similar way if my web page designer used special characters or spaces in field names. Also in theory, I could have multiple filed with the same name!
I have applied a further set of check on the contents of the field called search. This is done to ensure only simple literal searches to our web site visitor are offered, rather than allowing him to enter regular expressions. Of course, we're also removing the ability to search for special characters, but in a demonstration that's not really an issue.
When I echo out the value that was entered because it contains illegal search characters
, I have to encode characters which mean something special to the browser within the HTML stream = & and <. In a full application I would do this on every output; in this sample, I've not done it on the data that I read from the file (as I know my data file won't change and is clean) nor on my correcly-echoed search string as I've already filtered that. (written 2012-06-22, updated 2012-06-23)
Associated topics are indexed underR114 - Ruby on the Web 
Web and console - same principle, same code - Ruby example - (2013-02-14) 
Some TestWise examples - helping use Ruby code to check your web site operation - (2012-02-24) 
3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11) 
Ruby at both extremes of your website - (2011-09-10) 
Answers on Ruby on Rails - (2010-01-30) 
Ruby on Rails - a sample application to teach you how - (2010-01-30) 
Ruby to access web services - (2008-11-16)
Some other Articles
Multiple views in a single appication - sharing common parts of the template - Ruby on RailsSome traps it's so easy to fall into in designing your web siteAlan Turing - 1912 to 1954Melksham - a new dawnRuby on the web - a simple example using CGIHello World - Ruby on Rails - a checklist of each stepFine evening, country walk from Melksham - picturesSample answers to training course exercises - available on our web siteMuttable v immutable and implications - RubyMelksham Chamber of Commerce - looking to our future shape. Pivotal meeting next Tuesday
4088 posts, page by page
Link to page ... 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 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).