CGI newbie


#1

Hello,

I am starting a personal project to build a configuration webpage for a
small router device. Half of it is already done in perl cgi by someone
else. I want to rewrite that and add to it using ruby and cgi. Does
anyone have some good resources for rubg cgi programming? I am sorta
new to ruby and very new to cgi. Or possibly should I just stick with
the perl cgi that is already partially implemented?

Thank you
Sam


#2

This might help you:

http://lmgtfy.com/?q=ruby+cgi+tutorial&l=1

:wink:

On Mar 5, 2009, at 1:27 PM, Sam wrote:

Thank you
Sam

Best regards,


#3

Sam wrote:

Hello,

I am starting a personal project to build a configuration webpage for a
small router device. Half of it is already done in perl cgi by someone
else. I want to rewrite that and add to it using ruby and cgi. Does
anyone have some good resources for rubg cgi programming? I am sorta
new to ruby and very new to cgi. Or possibly should I just stick with
the perl cgi that is already partially implemented?

Speaking from copious personal experience with raw CGI in both Perl and
Ruby…

…Rewrite the app using Ruby on Rails. You will spend very little time
twiddling with the raw HTTP messages, and you will find re-implementing
the
actual logic very easy.

If you try to replicate this program in Ruby, it would not be much of an
improvement over Perl.


#4

Phlip wrote:

http://lmgtfy.com/?q=ruby+cgi+tutorial&l=1

:wink:

For future reference, posting that thing is the equivalent of saying “I
am too stupid to be able to exceed Google’s average answer for this
newsgroup’s topic.”

So anyone who doesn’t mind declaring themselves to be idiots, by all
means keep posting it!

I google a Rails error message the other day, and the first thing that
came up was a mailing-list response with nothing but an lmgtfy link.
You know, it was funny once or twice, but now it’s just Rick-rolling.


#5

Sam wrote:

Thank you
Sam

Considering you don’t know Ruby or understand CGI (which is something
you can probably learn in minutes or hours), you might not want to try
and re-do it just for the sake of it, if you don’t know the language or
interface you’ll be using well. If it works now, there’s no reason to
change it, though I understand if you want to use it as a project to
help you learn.


#6

http://lmgtfy.com/?q=ruby+cgi+tutorial&l=1

:wink:

For future reference, posting that thing is the equivalent of saying “I
am too
stupid to be able to exceed Google’s average answer for this newsgroup’s
topic.”

So anyone who doesn’t mind declaring themselves to be idiots, by all
means keep
posting it!


#7

Sam wrote:

I want to rewrite that and add to it using ruby and cgi. Does
anyone have some good resources for rubg cgi programming?

http://www.ruby-doc.org/docs/ProgrammingRuby/

Then click on “Ruby and the Web”

However as others have said, a monolithic CGI which combines both the
application logic and the HTML is likely to suck. Look at Rails or
Sinatra - the first if you like big apps which do everything for you,
but you have to hunt through the docs to find what the feature you’re
looking for you, and the latter for something small and easy to
understand, but you have to add in bits of functionality as you need
them.


#8

Phlip wrote:

http://lmgtfy.com/?q=ruby+cgi+tutorial&l=1

:wink:

For future reference, posting that thing is the equivalent of saying
“I am too stupid to be able to exceed Google’s average answer for this
newsgroup’s topic.”

Or it’s the equivalent of saying “Your question is so simple it could be
answered with a Google search, yet you didn’t give us the courtesy of
going that far, and instead chose to bother hundreds of people with YOUR
problem.”

And let’s face it, that link is a lot tamer than, say:

http://justfuckinggoogleit.com/

If you want an answer that exceeds Google’s first few search results,
ask a more intelligent question in the first place, and ask it after
making a decent effort to find the answer yourself. In fact, here’s a
detailed guide on how to ask:

http://catb.org/~esr/faqs/smart-questions.html

Common points that a few newbies every day seem to miss:

  • Try to find the answer yourself
    There’s Google, there’s archives, there’s manuals, there’s FAQs.
    Those exist for a reason – to save your time and ours. Even if you
    can’t find it, showing that you at least made an effort will get you a
    bit more respect than “Help, I’m a newbie!”

  • Choose your forum carefully
    Ruby is not Rails. Ask on the Rails group for Rails questions.

  • Follow common etiquette for that forum
    Most of the people who care hate top-posting. If you can’t be
    bothered to spend a few seconds pressing the fscking arrow keys, why
    should I bother to spend a few seconds reading your post?

  • Don’t post homework questions
    There seems to be a lot of these on this forum, and they get
    answered, unfortunately…
    Look, I’m not against helping, but if you’re clearly asking me to do
    your job for you, I feel like I should be asking to get paid. And if
    this actually is your homework (or your job), from some of the questions
    asked, I hope you get fired (or fail), and go back to school (or take
    the class over) – not because I hate you, but because your employer
    (current or future) deserves better than a Paula Bean:

http://thedailywtf.com/articles/the_brillant_paula_bean.aspx

This particular question is one of a pattern: “I’m a newbie at foo.”
Clearly you are, what was the question? “How do I learn foo?” Sigh…

For future reference, keep in mind that you are asking a favor from
quite possibly hundreds of people you’ve never met. Meet us halfway.
It’s probably faster to STFW anyway than it is to ask us – so it’s not
just inconsiderate laziness, it’s foolish, self-defeating, inconsiderate
laziness.

And here’s a hint: If you at least say “I looked, but I couldn’t seem to
find a good tutorial. This page looks outdated…” That at least
shows you tried, and is likely to get a friendlier response.

By the way, I have no problem being called out if I start asking stupid
questions, too. But frankly, I think a harsh answer like lmgtfy, or even
justfuckinggoogleit or STFW, is better than no answer at all. Better for
the user, because they might get their question answered, and they might
even pick up some Google skills. Better for us, because the same person
won’t be back tomorrow asking the exact same question, wondering why it
never gets answered.


#9

Sam wrote:

Maybe I should have clarified this “app” better before.

Probably.

So pretty simple stuff like writing to text files and restarting
services. I only have 300mhz and 128mb of ram to work with so I don’t
know if RoR would be the best idea?

I would guess Rails would still work, but a better choice might be
Sinatra, or even Merb if you want a full stack. I am also guessing that
traditional CGI is still not the best choice.

I’m going to say Sinatra and Ruby 1.9.1 are probably what you want.


#10

Brian C. wrote:

Sinatra - the first if you like big apps which do everything for you,
but you have to hunt through the docs to find what the feature you’re
looking for you, and the latter for something small and easy to
understand, but you have to add in bits of functionality as you need
them.

Yes that is about the best example that I have found. Unfortunately
searching google for “ruby cgi” yields many websites all with that same
example (hence why I posted on here). Maybe I should have clarified
this “app” better before. I need to make a couple of webpages for a
small embedded router/bridge that will configure squid proxy and
dansguardian. So pretty simple stuff like writing to text files and
restarting services. I only have 300mhz and 128mb of ram to work with
so I don’t know if RoR would be the best idea? After spending most of
today reading, I think my main weak spot is just html form/input
processing since I have never done that before. I guess I might have to
make a trip to the book store…

Sam


#11

David M. wrote:

So pretty simple stuff like writing to text files and restarting
services. I only have 300mhz and 128mb of ram to work with so I don’t
know if RoR would be the best idea?

I would guess Rails would still work, but a better choice might be
Sinatra, or even Merb if you want a full stack. I am also guessing that
traditional CGI is still not the best choice.

I’m going to say Sinatra and Ruby 1.9.1 are probably what you want.

I second that. The HTML form processing is already included. Try this
for starters:

require ‘rubygems’
require ‘sinatra’

get ‘/’ do
haml :index
end

post ‘/update’ do
STDERR.puts params.inspect # <= it’s all in the params hash

do some work

redirect ‘/’
end

END
@@ index
%h1 Hello
%p
OK, so it’s a bit unfair of me to tell
a newbie to dive straight in with HAML.
But now I find this easier than form helpers.
%form{:action=>"/update",:method=>“post”}
%label
Type something here:
%input{:type=>“text”,:name=>“foo”}
%label
Select one of these:
%select{:name=>“bar”}
%option One
%option Two
%option Three
%input{:type=>“submit”,:value=>“go”}


#12

Brian C. wrote:

I second that. The HTML form processing is already included. Try this
STDERR.puts params.inspect # <= it’s all in the params hash
But now I find this easier than form helpers.
%input{:type=>“submit”,:value=>“go”}
Interesting… Thanks!

Sam


#13

Brian C. wrote:

I’m going to say Sinatra and Ruby 1.9.1 are probably what you want.

I second that.

Actually, I second Sinatra, but not Ruby 1.9.1. (Although the version of
Sinatra released a few days ago claims to be 1.9-compatible, I believe)


#14

I’m going to say Sinatra and Ruby 1.9.1 are probably what you want.

I second that. The HTML form processing is already included.

Since this was a newbie question about writing CGI apps with ruby: If
I’m not mistaken Sinatra is supposed to work as CGI script via rack –
at least that’s what the blurb says. Does anyone happen to know a
Sinatra newbie + rack for CGI tutorial? The last time I tried I didn’t
get the impression this was something anyone has ever tried to do
before. Is it possible? Is it useful?


#15

On Thu, Mar 5, 2009 at 8:27 AM, Sam removed_email_address@domain.invalid wrote:

Hello,

I am starting a personal project to build a configuration webpage for a
small router device. Â Half of it is already done in perl cgi by someone
else. Â I want to rewrite that and add to it using ruby and cgi. Â Does anyone
have some good resources for rubg cgi programming? Â I am sorta new to ruby
and very new to cgi. Â Or possibly should I just stick with the perl cgi that
is already partially implemented?

On that last question, the answer depends on WHY you are doing this.
If the main reason is “I need a new configuration webpage for my
router with functionality X, Y, and Z, and I need it as soon as
possible, and it needs to be bug free”, and you already know Perl more
than Ruby, there is a good chance that the best thing to do is to
update the Perl without changing languages.

On the other hand, if you want to learn Ruby and have an interest in
using it for web programming, and you are willing to accept the
probability that you’ll expend more time and effort to finish the
project but also learn something about Ruby in the process, it may
make sense to switch to Ruby.


#16

Thanks Listening wrote:

I’m going to say Sinatra and Ruby 1.9.1 are probably what you want.

I second that. The HTML form processing is already included.

Since this was a newbie question about writing CGI apps with ruby: If
I’m not mistaken Sinatra is supposed to work as CGI script via rack –
at least that’s what the blurb says. Does anyone happen to know a
Sinatra newbie + rack for CGI tutorial? The last time I tried I didn’t
get the impression this was something anyone has ever tried to do
before. Is it possible? Is it useful?

I tried it. It works, but:

  • you need the most recent Sinatra (0.9.1)

  • you need rack HEAD from git

  • you need to add
    set :server,“cgi”
    since CGI isn’t (yet) autodetected

  • you need to beware that your links are relative to your CGI, so
    instead of

    %form{:action=>"/update", …}
    you need
    %form{:action=>"/cgi-bin/first-sinatra.rb/update", …}

This is where having a link helper method which prepends
env[“SCRIPT_NAME”] is useful.

  • startup time can be improved by removing ‘require “rubygems”’ and
    explicitly adding paths:

$:.unshift “/path/to/sinatra/lib”
$:.unshift “/path/to/rack/lib”
$:.unshift “/path/to/haml/lib”