Forum: Ruby CGI newbie

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0babf177db6fc39a4460aea7b2901d08?d=identicon&s=25 Sam (Guest)
on 2009-03-05 17:29
(Received via mailing list)
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
79caabff3b239b1bee6de8c51bc7f78c?d=identicon&s=25 Rolando Abarca (Guest)
on 2009-03-05 20:07
(Received via mailing list)
This might help you:

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

;-)

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

> Thank you
> Sam


Best regards,
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-05 20:30
(Received via mailing list)
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.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-05 20:35
(Received via mailing list)
> http://lmgtfy.com/?q=ruby+cgi+tutorial&l=1
>
> ;-)

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!
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 Jeff Schwab (Guest)
on 2009-03-05 20:52
(Received via mailing list)
Phlip wrote:
>> http://lmgtfy.com/?q=ruby+cgi+tutorial&l=1
>>
>> ;-)
>
> 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.
F065301eb65a5d0da8edcb8de9d5e28e?d=identicon&s=25 Tim Greer (Guest)
on 2009-03-05 20:54
(Received via mailing list)
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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-03-05 22:58
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.
1bc63d01bd3fcccc36fb030a62039352?d=identicon&s=25 David Masover (Guest)
on 2009-03-06 00:45
(Received via mailing list)
Phlip wrote:
>> http://lmgtfy.com/?q=ruby+cgi+tutorial&l=1
>>
>> ;-)
>
> 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 <link> 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.
0babf177db6fc39a4460aea7b2901d08?d=identicon&s=25 Sam (Guest)
on 2009-03-06 01:42
(Received via mailing list)
Brian Candler 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
1bc63d01bd3fcccc36fb030a62039352?d=identicon&s=25 David Masover (Guest)
on 2009-03-06 21:13
(Received via mailing list)
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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-03-06 22:26
David Masover 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"}
0babf177db6fc39a4460aea7b2901d08?d=identicon&s=25 Sam (Guest)
on 2009-03-07 05:52
(Received via mailing list)
Brian Candler 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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-03-07 14:08
Brian Candler 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)
Be4356769d2f02a1330e902fa675d763?d=identicon&s=25 Thanks Listening (t4l)
on 2009-03-07 16:43
>> 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?
D7463bd611f227cfb2ef4da4a978a203?d=identicon&s=25 Christopher Dicely (Guest)
on 2009-03-07 20:14
(Received via mailing list)
On Thu, Mar 5, 2009 at 8:27 AM, Sam <ruby@net153.net> 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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-03-07 21:28
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"
This topic is locked and can not be replied to.