How to run simple Ruby on a local web server

I am using WEBrick to run a local server on server 2000 and am trying to run Ruby with ERB. (I have Ruby 1.9.2 installed in my OS X box). This does not seem to work.

For example, I created an erb file (tryErb.erb) as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>try erb</title>
</head>
<body>
    <p> % 99.downto(96) do |number|
    <%= number %> bottles of beer…
    % end
    </p>
</body>
</html>

I used chmod a=rwx tryErb.erbto make an executable, but if I try to visit the correct URL (http: // localhost: 2000 / tryErb.erb), nothing works. The browser is not going anywhere; it just stays on the root index. The log in bash shows:

localhost - - [12 / May / 2011: 10: 12: 05 CEST] "GET / tryErb.erb HTTP / 1.1" 200 199 http: // localhost: 2000 / → /tryErb.erb

Any ideas?

+3
source share
1

, , . mime: MimeTypes = > {'rhtml' = > 'text/html'}, http://www.ruby-forum.com/topic/96436, rhtml ruby, <%...% > .

require 'webrick'

include WEBrick

def start_webrick(config = {})
    # always listen on port 3000
    config.update(:Port => 3000)
    config.update(:MimeTypes => {'rhtml' => 'text/html'})
    server = HTTPServer.new(config)
    yield server if block_given?
    ['INT', 'TERM'].each {|signal| 
        trap(signal) {server.shutdown}
    }
    server.start
end

start_webrick(:DocumentRoot => Dir::pwd)
+4

All Articles