# Write webpages in Ruby without Rails? - Newb Question

**URL:** <https://www.ruby-forum.com/t/write-webpages-in-ruby-without-rails-newb-question/119449>\
**Category:** Ruby\
**Created:** [November 7, 2007, 2:04am UTC](https://www.ruby-forum.com/t/write-webpages-in-ruby-without-rails-newb-question/119449 "2007-11-07T02:04:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgossen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rgossen](https://www.ruby-forum.com/u/rgossen)\
**Post date:** [November 7, 2007, 2:04am UTC](https://www.ruby-forum.com/t/write-webpages-in-ruby-without-rails-newb-question/119449/1 "2007-11-07T02:04:49Z")

</div>

I am a programming newb and I just bought the book “Software  
Engineering for Internet Applications,” and I need a server to go  
through the labs. Unfortunately, the extent of my server experience  
is using script/server in Rails. To do these labs, I need to write  
webpages in Ruby without Rails (or any framework), but I am not sure  
exactly how to go about it.

I started to use the Apache 1.3 webserver that comes with os x and  
attempted to install eruby. That doesn’t seem to be working. I am  
thinking there has to be an easier way. Is there a way for me to use  
Mongrel or Webrick outside of Rails? I initially installed everything  
using the Hivelogic instructions.

---

<div class="post-metadata">

**Author:** ![rgossen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rgossen](https://www.ruby-forum.com/u/rgossen)\
**Post date:** [November 7, 2007, 2:11am UTC](https://www.ruby-forum.com/t/write-webpages-in-ruby-without-rails-newb-question/119449/2 "2007-11-07T02:11:59Z")

</div>

rgossen wrote:

> I am a programming newb and I just bought the book “Software  
> Engineering for Internet Applications,” and I need a server to go  
> through the labs. Unfortunately, the extent of my server experience  
> is using script/server in Rails. To do these labs, I need to write  
> webpages in Ruby without Rails (or any framework), but I am not sure  
> exactly how to go about it.
> 
> I started to use the Apache 1.3 webserver that comes with os x and  
> attempted to install eruby. That doesn’t seem to be working. I am  
> thinking there has to be an easier way. Is there a way for me to use  
> Mongrel or Webrick outside of Rails? I initially installed everything  
> using the Hivelogic instructions.

well, you can write HTML like a string, then have the output be an HTML  
file. So,

html\_file = File.new(“C:\web\_pages\index.html”, “w+”)

html\_file.write("\n\nOMG THIS IS GOING TO TAKE A LONG  
TIME\n\n")

html\_file.close

Hopefully someone has a better idea 🙂

Good luck

~Jeremy

---

<div class="post-metadata">

**Author:** ![rgossen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rgossen](https://www.ruby-forum.com/u/rgossen)\
**Post date:** [November 7, 2007, 2:20am UTC](https://www.ruby-forum.com/t/write-webpages-in-ruby-without-rails-newb-question/119449/3 "2007-11-07T02:20:02Z")

</div>

This is really a question for the rails-talk list, but since I am  
right now in rails…

I would create a rails application and a default controller and index  
action. Then you can just write Ruby inside a \<% %\> pair in the  
index.rhtml file.

I am sure you’ll get a dozen responses about how to set it up simpler,  
but that’s the way I know you can get it to work from where you are.

–Colin

---

<div class="post-metadata">

**Author:** ![rgossen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rgossen](https://www.ruby-forum.com/u/rgossen)\
**Post date:** [November 7, 2007, 4:04am UTC](https://www.ruby-forum.com/t/write-webpages-in-ruby-without-rails-newb-question/119449/4 "2007-11-07T04:04:06Z")

</div>

On Nov 6, 7:51 pm, rgossen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Mongrel or Webrick outside of Rails? I initially installed everything  
> using the Hivelogic instructions.

Check out webrick. Here is a simple script that will do a directory  
listing on wherever you run it. Just open up your browser to port  
2000.

Now, to get it to actually output html content-types and all, will  
require some more work. But, perhaps this will get you started.

#!/bin/env ruby  
require ‘webrick’  
include WEBrick

s = HTTPServer.new(  
:Port =\> 2000,  
:DocumentRoot =\> Dir::pwd  
)

trap(“INT”){ s.shutdown }  
s.start

---

<div class="post-metadata">

**Author:** ![rgossen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rgossen](https://www.ruby-forum.com/u/rgossen)\
**Post date:** [November 7, 2007, 4:15am UTC](https://www.ruby-forum.com/t/write-webpages-in-ruby-without-rails-newb-question/119449/5 "2007-11-07T04:15:47Z")

</div>

I would recommend the “serve” gem

[http://wiseheartdesign.com/2007/9/25/introducing-serve/](http://wiseheartdesign.com/2007/9/25/introducing-serve/)

It makes it simple to code and serve webpages with ruby.

But there are lots of options for generating HTML code using ruby,  
check [rubyforge.org](http://rubyforge.org) for gems like “markaby” or “xx”.

You can serve page with Mongrel like this:

require ‘rubygems’  
gem ‘mongrel’  
require ‘mongrel’

HOST = ‘0.0.0.0’  
PORT = 999

config = Mongrel::Configurator.new :host =\> HOST, :port =\> PORT do  
listener do  
uri “/”, :handler =\> Mongrel::DirHandler.new(Dir.pwd)  
end

trap(“INT”) { stop }  
run  
end

puts “Mongrel running on #{HOST}:#{PORT} with docroot #{Dir.pwd}”  
config.join

---

<div class="post-metadata">

**Author:** ![rgossen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rgossen](https://www.ruby-forum.com/u/rgossen)\
**Post date:** [November 7, 2007, 3:55am UTC](https://www.ruby-forum.com/t/write-webpages-in-ruby-without-rails-newb-question/119449/6 "2007-11-07T03:55:20Z")

</div>

On 11/6/07, rgossen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Mongrel or Webrick outside of Rails? I initially installed everything  
> using the Hivelogic instructions.

I haven’t played with Ruby and web outside of frameworks (Rails,  
Camping, etc.), but you can use Apache with mod\_ruby I think. Your  
files will probably have to take on the eruby syntax of \<% %\>  
encapsulating code tags, which would be similar to other interactive  
web code (ala CodeFusion and others).

As far as I can tell, Mongrel knows how to feed a web server( like  
Apache), but is not necessarily a complete web server itself.  
Somebody correct me if I’m wrong, because I can’t seem to find any  
straight example of somebody just using Mongrel with Ruby by  
themselves to serve pages!

thx,  
Todd

---

<div class="post-metadata">

**Author:** ![rgossen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rgossen](https://www.ruby-forum.com/u/rgossen)\
**Post date:** [November 7, 2007, 5:52am UTC](https://www.ruby-forum.com/t/write-webpages-in-ruby-without-rails-newb-question/119449/7 "2007-11-07T05:52:48Z")

</div>

On Nov 6, 8:14 pm, Nathan G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would recommend the “serve” gem
> 
> [http://wiseheartdesign.com/2007/9/25/introducing-serve/](http://wiseheartdesign.com/2007/9/25/introducing-serve/)
> 
> It makes it simple to code and serve webpages with ruby.

Wow, this looks perfect. I was not familiar with haml, but after  
looking at the tutorial, it looks like a great alternative to rhtml  
for templating. Thanks so much for the tip.

---

<div class="post-metadata">

**Author:** ![rgossen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rgossen](https://www.ruby-forum.com/u/rgossen)\
**Post date:** [November 7, 2007, 9:05am UTC](https://www.ruby-forum.com/t/write-webpages-in-ruby-without-rails-newb-question/119449/8 "2007-11-07T09:05:47Z")

</div>

On Wed, Nov 07, 2007 at 11:53:37AM +0900, Todd B. wrote:

> As far as I can tell, Mongrel knows how to feed a web server( like  
> Apache), but is not necessarily a complete web server itself.  
> Somebody correct me if I’m wrong, because I can’t seem to find any  
> straight example of somebody just using Mongrel with Ruby by  
> themselves to serve pages!

You might want to give heel a try, that’s pretty much what it is,  
mongrel with  
some mime-type sugar and nice directory listings.

```
% sudo gem install heel
% heel
** Signals ready. TERM => stop. USR2 => restart. INT => stop (no 

```

restart).  
\*\* heel running at [http://127.0.0.1:4331](http://127.0.0.1:4331) with document root  
/home/jeremy/proj  
\*\* Use Ctrl-C to stop.  
\*\* Launching your browser…  
127.0.0.1 - - [07/Nov/2007:00:58:17 MST] “GET / HTTP/1.1” 200 46615  
127.0.0.1 - - [07/Nov/2007:00:58:17 MST] “GET /icons/folder.png  
HTTP/1.1” 200 537  
127.0.0.1 - - [07/Nov/2007:00:58:17 MST] “GET /icons/application.png  
HTTP/1.1” 200 464

enjoy,

-jeremy
