Hi. I apologize in advance if this is a dumb question, but though I've searched online I haven't found an answer. I'm starting to teach my 8yr daughter Ruby. We've covered HTML and CSS already and she handcoded her own static website. I'd like to incorporate her Ruby learning experience into her website, which is much more interesting for her than using Ruby to write scripts or desktop apps. She's just starting out, so Rails is much too complicated for her. She's not ready for the whole MVC concept yet. What I'd like to do is to run very simple Ruby scripts from the site and incorporate Ruby code into rhtml files, but without Rails. I don't really want a "framework", just the ability to run a ruby file that will serve up an rhtml file. In other words, something very simple like PHP (I don't want to teach her PHP). [I'll get into Rails later once she's more advanced.] ERb looks like the ticket, but there's a missing link (for me). I can write a ruby script that generates an rhtml file, but then how do I serve that rhtml file without a framework like Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow, but I'm missing the connection. I can't find a simple tutorial anywhere online, and I'm no expert on web applications. Thanks for the help.
on 2006-09-20 18:01
on 2006-09-20 18:17
On 9/20/06, zerohalo <zerohalo@gmail.com> wrote: > Ruby code into rhtml files, but without Rails. I don't really want a > online, and I'm no expert on web applications. > > Thanks for the help. > > > Webrick has WEBrick::HTTPServlet::ERBHandler, which (I believe) serves ERb pages. Mongrel probably has something similar.
on 2006-09-20 18:27
zerohalo wrote: / ... > ERb looks like the ticket, but there's a missing link (for me). I can > write a ruby script that generates an rhtml file, but then how do I > serve that rhtml file without a framework like > Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow, > but I'm missing the connection. I can't find a simple tutorial anywhere > online, and I'm no expert on web applications. Just one question. Do you want the existing Web server to serve your Ruby code, or do you require the server to be Ruby-based also? If the former, you can tell Apache to serve Ruby scripts that contain the usual CGI conventions. That seems simple enough. And it might not be what you are asking for.
on 2006-09-20 18:27
On 9/20/06, zerohalo <zerohalo@gmail.com> wrote: > Ruby code into rhtml files, but without Rails. I don't really want a > online, and I'm no expert on web applications. > > Thanks for the help. Webrick seems to have support for ERB - see httpservlet/erbhandler.rb, that is used for .rhtml files by FileHandler. Just find in the tutorials how to start webrick for a given document root (Webrick::HttpServer.new({:DocumentRoot = '/var/www/'}) and something?) You can use meta_vars and query variables in your rhtml files.
on 2006-09-20 18:41
Paul Lutus wrote: > Just one question. Do you want the existing Web server to serve your Ruby > code, or do you require the server to be Ruby-based also? > No, the server doesn't need to be Ruby-based. I could use lighttpd or apache to serve the code. > If the former, you can tell Apache to serve Ruby scripts that contain the > usual CGI conventions. That seems simple enough. And it might not be what > you are asking for. That's the part I'm missing. I find Apache extremely difficult to configure (beyond the basics). How would I tell Apache to serve Ruby scripts? Thanks.
on 2006-09-20 18:53
On 9/20/06, zerohalo <zerohalo@gmail.com> wrote: > > usual CGI conventions. That seems simple enough. And it might not be what > > you are asking for. > > That's the part I'm missing. I find Apache extremely difficult to > configure (beyond the basics). How would I tell Apache to serve Ruby > scripts? In a recent thread (eruby?) someone mentioned adding > AddType application/x-httpd-eruby .rhtml > Action application/x-httpd-eruby /cgi-bin/eruby to httpd.conf In my win installation there is c:/ruby/bin/erb.bat so I suppose there is something similar in unix. Now either add #!/usr/bin/erb to the top of any .cgi file or, preferably, do something similar to the above, somehow telling apache to run erb. (I'm not an apache guru, so these are just hints to get you started...)
on 2006-09-20 18:59
Jan Svitok wrote: > Webrick seems to have support for ERB - see httpservlet/erbhandler.rb, > that is used for .rhtml files by FileHandler. Just find in the > tutorials how to start webrick for a given document root > (Webrick::HttpServer.new({:DocumentRoot = '/var/www/'}) and > something?) Yes, I tried that. Unfortunately there is no documentation on the webrick site on how to use ERBHandler. I have this in a ruby script which does start up the Webrick server. However, while it serves HTML files fine, it treats RHTML files as binaries and RB files as just plain text. Here's my code: <pre><code> require 'webrick' include WEBrick def start_webrick(config = {}) config.update(:Port => 8080) server = HTTPServer.new(config) yield server if block_given? ['INT', 'TERM'].each {|signal| trap(signal) {server.shutdown} } ruby_dir = File.expand_path('/data/sandbox/ruby') server.mount("/data/sandbox/ruby", HTTPServlet::ERBHandler, ruby_dir) server.start end start_webrick(:DocumentRoot => '/data/sandbox/ruby') </code></pre>
on 2006-09-20 19:19
On 9/20/06, zerohalo <zerohalo@gmail.com> wrote: > Yes, I tried that. Unfortunately there is no documentation on the > webrick site on how to use ERBHandler. I have this in a ruby script > which does start up the Webrick server. However, while it serves HTML > files fine, it treats RHTML files as binaries and RB files as just > plain text. Here's my code: require 'webrick' include WEBrick def start_webrick(config = {}) config.update(:Port => 8080) + config.update(:MimeTypes => {'rhtml' => 'text/html'}) server = HTTPServer.new(config) yield server if block_given? ['INT', 'TERM'].each {|signal| trap(signal) {server.shutdown} } - ruby_dir = File.expand_path('/data/sandbox/ruby') - server.mount("/data/sandbox/ruby", HTTPServlet::ERBHandler, ruby_dir) server.start end start_webrick(:DocumentRoot => '/data/sandbox/ruby')
on 2006-09-20 19:37
Thanks, Jan. That was easy. Now .rhtml files render correctly. Is there a way to tell it to execute .rb files rather than treat them as simple text files?
on 2006-09-20 19:43
On 9/20/06, zerohalo <zerohalo@gmail.com> wrote: > [snip] > > > If the former, you can tell Apache to serve Ruby scripts that contain the > > usual CGI conventions. That seems simple enough. And it might not be what > > you are asking for. > > That's the part I'm missing. I find Apache extremely difficult to > configure (beyond the basics). How would I tell Apache to serve Ruby > scripts? On Debian and Ubuntu, it just works. After you apt-get install apache2, you just copy some foo.rb file (maybe like the one shown below) to /usr/lib/cgi-bin/ and then point your browser at http://localhost/cgi-bin/foo.rb and boom. :) ==== snip /usr/lib/cgi-bin/foo.rb ==== #!/opt/ruby/bin/ruby -w puts "Content-type: text/html" puts puts "<html><body> <p>The time is now #{Time.now}, so you'd better get crackin'! </p> </body> </html>" ==== /snip ==== ---John
on 2006-09-20 20:01
On 9/20/06, zerohalo <zerohalo@gmail.com> wrote: > > > files fine, it treats RHTML files as binaries and RB files as just > > > plain text. Here's my code: > > > > require 'webrick' > > include WEBrick + module WEBrick + module HTTPServlet + FileHandler.add_handler("rb", CGIHandler) + end + end > > - server.mount("/data/sandbox/ruby", HTTPServlet::ERBHandler, ruby_dir) > > server.start > > end > > > > start_webrick(:DocumentRoot => '/data/sandbox/ruby') Now it starts .rb for me, although I'm not able to write any good cgi right now ;-) maybe it's because I'm on windows...
on 2006-09-20 20:11
On 9/20/06, zerohalo <zerohalo@gmail.com> wrote: > I don't really want a > "framework", just the ability to run a ruby file that will serve up an > rhtml file. In other words, something very simple like PHP (I don't > want to teach her PHP). [I'll get into Rails later once she's more > advanced.] Does this help? http://cyll.org/blog/tech/2006-08-26-makingrubyintophp.html
on 2006-09-20 20:41
Jan Svitok wrote: > Now it starts .rb for me, although I'm not able to write any good cgi > right now ;-) maybe it's because I'm on windows... Doesn't work for me. With even the simplest .rb file it gives me the following error: ERROR Premature end of script headers: but maybe I need to put a particular include in particular at the top of each .rb file? Not used to working with .rb files outside Rails.
on 2006-09-20 21:01
Wilson Bilkovich wrote: > Does this help? > http://cyll.org/blog/tech/2006-08-26-makingrubyintophp.html Indeed it did. Thanks. That works nicely.
on 2006-09-20 21:51
zerohalo wrote: > > Jan Svitok wrote: > >> Now it starts .rb for me, although I'm not able to write any good cgi >> right now ;-) maybe it's because I'm on windows... > > Doesn't work for me. With even the simplest .rb file it gives me the > following error: > > ERROR Premature end of script headers: This is a classic CGI header error, caused by the absence of a recognizable emitted header like: print "Content-type: text/html\r\n\r\n" As the first text emitted by the script. Remember, if you can get the server to read the filetype and treat it as a well-behaved source, and if you can get the Ruby scripts to emit the expected header information, your server requirements become much simpler to meet.
on 2006-09-20 22:01
zerohalo wrote: >> usual CGI conventions. That seems simple enough. And it might not be what >> you are asking for. > > That's the part I'm missing. I find Apache extremely difficult to > configure (beyond the basics). How would I tell Apache to serve Ruby > scripts? Others have offered good advice. The simplest way is to give the server what it expects to see: --------------------------------------- #!/usr/bin/ruby -w print "Content-type: text/html\r\n\r\n" print "<html><body><center><h1>Hello world!</h1></center></body></html>" --------------------------------------- Put this file in a directory where executable content is expected to be, like /serverpath/cgi-bin, and chances are Apache will treat it as you would expect, without any special configuration.
on 2006-09-20 22:31
Paul Lutus wrote: > This is a classic CGI header error, caused by the absence of a recognizable > emitted header like: > > print "Content-type: text/html\r\n\r\n" > > As the first text emitted by the script. Got it. That works, thanks, Paul, and Jan, for your patience with my ignorance.
on 2006-09-21 02:20
zerohalo wrote: > Hi. Hi. > She's just starting out, so Rails is much too complicated > for her. She's not ready for the whole MVC concept yet. FWIW, I have the brain of an 8yr old in when it comes to web-aps, and I managed to write a /couple/ Camping aps the other evening. Regards,
on 2006-09-21 03:40
François Montel wrote: > Got it. That works, thanks, Paul, and Jan, for your patience with my > ignorance. It seems like I am at the same level as your daughter [although a whole 9 years older] and am trying to do the same thing you are. I could not get anything working from what I gathered in this thread, but it seems you have. Can you tell me what you ended up doing so I can do that too? thanks
on 2006-09-21 03:44
Bil Kleb wrote: > > FWIW, I have the brain of an 8yr old in when it comes > to web-aps, and I managed to write a /couple/ Camping aps > the other evening. > Fascinating. I've looked at Camping but haven't really grasped it yet. I'm not a web guy. I do it slowly and painfully. I also have the brain of an eight year old. I keep it in a jar on my desk. Hal
on 2006-09-21 03:56
Jonathan Denni wrote: > François Montel wrote: > >> Got it. That works, thanks, Paul, and Jan, for your patience with my >> ignorance. > > It seems like I am at the same level as your daughter [although a whole > 9 years older] and am trying to do the same thing you are. I could not > get anything working from what I gathered in this thread, but it seems > you have. Can you tell me what you ended up doing so I can do that too? It's very simple, really. Apache requires that executable pages (pages that have to be processed by an interpreter like Ruby) by placed in a special directory. The default for this is usually /sitedirectry/cgi-bin. Write an ordinary Ruby script, but make sure the first thing it prints is a CGI header, like this: print "Content-type: text/html\r\n\r\n" After this first line, just output HTML, as in my prior example, and the page will appear to all intents and purposes as though it contains the text it is printing. Therefore your Ruby code decides what the page content is, and it is obviously dynamic. A Ruby interpreter must be installed on the server machine. Apart from that, it should work without any fuss.
on 2006-09-21 05:06
zerohalo wrote: > Ruby code into rhtml files, but without Rails. I don't really want a > online, and I'm no expert on web applications. I've an example of this that's ~100 lines of code, if you're interested. Maps URLs to classes and templates and renders them, using erb and Webrick. -- James Britt "A principle or axiom is of no value without the rules for applying it." - Len Bullard
on 2006-09-21 05:38
On Sep 20, 2006, at 8:05 PM, James Britt wrote: >> complicated >> write a ruby script that generates an rhtml file, but then how do I > Maps URLs to classes and templates and renders them, using erb and > Webrick. > > > > -- > James Britt I'd be interested in seeing that James. Thanks -Ezra
on 2006-09-21 05:49
Paul Lutus wrote: > A Ruby interpreter must be installed on the server machine. Apart from > that, > it should work without any fuss. ahh, well, thats probably my problem. i'm using the cheapest server i could find (canaca.com), which obviousely has no support for ruby. i was hoping there was some shortcut way i could use ruby on my website without having to change servers. is there some way to do that, or is it totally hopeless?
on 2006-09-21 09:16
Jonathan Denni wrote:
> totally hopeless?
I didn't realize this was an offsite, commercial server. Many
traditional
servers stay away from anything relatively new, to avoid instabilities.
Some commercial webservers allow you to install your own binaries. All
you
have to do is find out what kind of processor and OS are in use, just as
you would for your home computer in choosing a binary. Then drop the
appropriate binary into /usr/bin or another suitable location, and you
are
good to go.
But without this option or one like it, your instinct is correct -- you
wouldn't be able to run Ruby.
on 2006-09-21 13:41
This works fine: eRuby: The Erb Alternative to Embedded Ruby in HTML http://www.hiveminds.co.uk/node/3105 It is extremly easy to setup, I have tried this solution in 2 shared hosting services without a problem. -- Aníbal Rojas http://www.rubycorner.com
on 2006-09-21 17:07
Thanks a lot. I didn't get it working. I put erb.cgi and a .htaccess in /var/www/cgi-bin/ as directed in http://www.hiveminds.co.uk/node/3105 but I can't find out what kind of OS the server is using, and i cannot create/edit files or directories in /bin or /usr/bin at this point, it looks to me like i should give up trying to use ruby on that server. Thanks again for your help.
on 2006-09-21 17:58
Ezra Zygmuntowicz wrote: > > On Sep 20, 2006, at 8:05 PM, James Britt wrote: > > > I'd be interested in seeing that James. > Here ya go! http://jamesbritt.com/code/wb_erb.tgz Fairly simple, few features, demos a few concepts. -- James Britt "Simplicity of the language is not what matters, but simplicity of use." - Richard A. O'Keefe in squeak-dev mailing list
on 2006-09-21 18:18
On Sep 21, 2006, at 10:58 AM, James Britt wrote: > http://jamesbritt.com/code/wb_erb.tgz > > Fairly simple, few features, demos a few concepts. Interesting. Thanks for sharing. I'll try and pay you back at least some of the effort by addressing one of your TODOs. From your code: # Windows only. TODO: figure out # how to do the same thing on Mac and 'nix boxen. Thread.new { # Wait a bit for the server to start, then launch a Web browser # to show the default page sleep( 5 ) warn `start http://127.0.0.1:#{PORT}` } On Mac OS X, replace the word "start" with "open" and it will work. Hope that helps. James Edward Gray II
on 2006-09-21 18:34
On Thu, 21 Sep 2006, James Britt wrote: >> ERb looks like the ticket, but there's a missing link (for me). I can >> write a ruby script that generates an rhtml file, but then how do I >> serve that rhtml file without a framework like >> Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow, >> but I'm missing the connection. I can't find a simple tutorial anywhere >> online, and I'm no expert on web applications. > > I've an example of this that's ~100 lines of code, if you're interested. > > Maps URLs to classes and templates and renders them, using erb and Webrick. I'd like to see it, just because I might find it educational. I've also been wondering if framework support for this sort of thing, but very simple to setup, would be useful to people? Something akin to this: app.rb ----- require 'iowa' Iowa.run ----- app.cnf ----- socket: hostname: foo.bar.com port: 3000 application: dispatcher: class: StandardDispatcher mapfile: mapfile.cnf ----- mapfile.cnf: ----- :map: /index.html: Index /form.html: Myform /response.html: Myresponse ----- If one had an index.rhtml, myform.rhtml, and myresponse.rhtml, they would be mapped to classes Index, Myform, and Myresponse, and requests for the urls above would go to the class in question and be rendered as an erb template. You'd run it with: ruby app.rb -r webrick or ruby app.rb -r mongrel No other code or config necessary to serve those rhtml files. Would anyone use this ability (as I personally would not). Worth my time to make the above work? Kirk haines
on 2006-09-21 18:42
Jonathan Denni wrote: > > Thanks a lot. I didn't get it working. I put erb.cgi and a .htaccess in > /var/www/cgi-bin/ as directed in http://www.hiveminds.co.uk/node/3105 > but I can't find out what kind of OS the server is using, and i cannot > create/edit files or directories in /bin or /usr/bin Will any CGI script work in the /var/www/cgi-bin directory, for example one that relies on "sh" or Perl? If so, put your own CGI script in /var/www/cgi-bin that reveals things about the OS. ------------------------------------- #!/bin/sh echo -e "Content-type: text/html\r\n\r\n" data=`set` echo "<html><body><pre>$data</pre></body></html>" ------------------------------------- Don't leave this script in place, just use it temporarily to find out what's going on. > > at this point, it looks to me like i should give up trying to use ruby > on that server. Use the above script and change: data=`whereis ruby` > > Thanks again for your help. Just some ideas, and your conclusion may be correct.
on 2006-09-28 04:12
Paul Lutus wrote: > Don't leave this script in place, just use it temporarily to find out > what's > going on. how do i access the script output?
on 2006-09-28 04:18
Jonathan Denni wrote:
> how do i access the script output?
nevermind, I was able to try it but i just got an error message
on 2006-09-28 06:51
Jonathan Denni wrote: > Jonathan Denni wrote: > >> how do i access the script output? > > nevermind, I was able to try it but i just got an error message What error message? For most typical errors, it's because you put the script in a place where it cannot be executed. Also, in some cases the script has to be given executable permissions.
on 2006-09-30 14:26
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Jonathan Denni wrote: > ahh, well, thats probably my problem. i'm using the cheapest server i > could find (canaca.com), which obviousely has no support for ruby. i was > hoping there was some shortcut way i could use ruby on my website > without having to change servers. is there some way to do that, or is it > totally hopeless? > Serve (part) of your website from your PC over which you have control? If it's a personal website or to display some experiments, this should be sufficient. David Vallner -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFHmIwy6MhrS8astoRAg4fAJ91NJYACa7LhlQhq4XaCUfIDDM+1QCdHW+e ux5wCukpPCM6L7pHYk95d0M= =PhPZ -----END PGP SIGNATURE-----
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.