# Webrick

**URL:** <https://rubytalk.org/t/webrick/45202>\
**Category:** ruby-talk\
**Created:** [15 March 2008 11:20 UTC](https://rubytalk.org/t/webrick/45202 "2008-03-15T11:20:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![abc1](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@abc1](https://rubytalk.org/u/abc1)\
**Post date:** [15 March 2008 11:20 UTC](https://rubytalk.org/t/webrick/45202/1 "2008-03-15T11:20:05Z")

</div>

Hi,

i'm new to ruby and tried some code to get a small application.  
I used Webrick to get a http server:

require 'webrick'  
include WEBrick  
s = HTTPServer.new(:Port =\> 8000)

# Add a mime type for \*.rhtml files  
HTTPUtils::DefaultMimeTypes.store('rhtml', 'text/html')  
# Required for CGI on Windows; unnecessary on Unix/Linux  
s.config.store( :CGIInterpreter, "#{HTTPServlet::CGIHandler::Ruby}")  
# Mount servlets  
s.mount('/test', HTTPServlet::FileHandler, '/')  
# Trap signals so as to shutdown cleanly.  
['TERM', 'INT'].each do |signal|  
&nbsp;&nbsp;trap(signal){ s.shutdown }  
end

# Start the server and block on input.  
s.start

and i created an rhtml file:

\<html\>  
\<head\>  
&nbsp;&nbsp;\<title\>Ruby Test\</title\>  
\</head\>  
\<body\>  
\<h2\>Test\</h2\>

\<form\>  
&nbsp;&nbsp;\<% require 'mysql' %\>  
&nbsp;&nbsp;\<% my = Mysql::new("localhost", "root", "", "books\_development") %\>  
&nbsp;&nbsp;\<% res = my.query("select ID,Name from books Limit 10") %\>  
&nbsp;&nbsp;\<table\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<% res.each { |i| puts "\<tr\>\<td\> #{i} \</td\>\</tr\>" } %\>   
&nbsp;&nbsp;\</table\>  
\</form\>  
\</body\>  
\</html\>

Now the problem is that i can see the html in my browser but the result of embedded Ruby code only in my Konsole. Could anyone give me the right direction.

Thanks in advance  
dirk

--------------= Posted using GrabIt =----------------  
------= Binary Usenet downloading made easy =---------  
-= Get GrabIt for free from [http://www.shemes.com/](http://www.shemes.com/) =-

---

<div class="post-metadata">

**Author:** ![Eleanor\_McHugh](https://avatars.discourse-cdn.com/v4/letter/e/8c91f0/32.png) [@Eleanor\_McHugh](https://rubytalk.org/u/Eleanor_McHugh)\
**Post date:** [15 March 2008 11:45 UTC](https://rubytalk.org/t/webrick/45202/2 "2008-03-15T11:45:43Z")

</div>

> i'm new to ruby and tried some code to get a small application.  
> I used Webrick to get a http server:

\<snip /\>

> Now the problem is that i can see the html in my browser but the result of embedded Ruby code only in my Konsole. Could anyone give me the right direction.

Before serving the rhtml file to your web client you need to run ERb on it and capture the result, then serve it using a different method than HTTP::Servlet.

I can't remember the details off-hand but if you look at my Camping presentation at [http://slides.games-with-brains.net/](http://slides.games-with-brains.net/) you'll find some simple examples of rolling WEBrick servers and integrating ERb is fairly simple. I usually just add it into the String class as a handy instance method:

class String  
&nbsp;&nbsp;&nbsp;def apply\_erb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ERB.new(self).result  
&nbsp;&nbsp;&nbsp;end  
end

so when generating the output for the server you'd have a line roughly like:

my\_content.apply\_erb

where my\_content is the variable holding the rhtml page text, or a method that produces it.

Ellie

Eleanor McHugh  
Games With Brains

> **···**
>
> On 15 Mar 2008, at 11:20, abc- wrote:  
> ----  
> raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![B\_Randy](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@B\_Randy](https://rubytalk.org/u/B_Randy)\
**Post date:** [12 June 2008 13:21 UTC](https://rubytalk.org/t/webrick/45202/3 "2008-06-12T13:21:12Z")

</div>

Hello Ellie,

I use WEBrick and Erb too and I want get all data (a array whith 3  
columns) from a big FORM, maybe by the POST method, but I've no idea how  
to make this ☹

You make a reference to yours slides. I've tried many times to "join"  
the group and to download the file without success. And the delay of  
communication is long, long, so long !

Have you an other place to get your presentation, or the solution could  
you help me to find the right solution ? 🙂

Thanks.

Eleanor McHugh wrote:

> **···**
>
> > On 15 Mar 2008, at 11:20, abc- wrote:
> > 
> > > i'm new to ruby and tried some code to get a small application.  
> > > I used Webrick to get a http server:
> > 
> > I can't remember the details off-hand but if you look at my Camping  
> > presentation at [http://slides.games-with-brains.net/](http://slides.games-with-brains.net/) you'll find some  
> > simple examples of rolling WEBrick servers and integrating ERb is  
> > fairly simple.
> > 
> > I usually just add it into the String class as a handy  
> > instance method:
> > 
> > class String  
> > &nbsp;&nbsp;&nbsp;def apply\_erb  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ERB.new(self).result  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > so when generating the output for the server you'd have a line roughly  
> > like:
> > 
> > my\_content.apply\_erb
> > 
> > where my\_content is the variable holding the rhtml page text, or a  
> > method that produces it.
> > 
> > Ellie
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
