3

I'm using WEBrick to start a local server on port 2000 and trying to execute Ruby with ERB. (I have Ruby 1.9.2 installed on my OS X box). It doesn't seems to work.

For example, I created an erb file (tryErb.erb) like this:

<!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.erb to make the file executable, but if I try to visit the proper URL (http://localhost:2000/tryErb.erb), nothing seems to work. The browser doesn't go anywhere; it simply stays on the root's index. The log in the bash shell shows:

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

Any ideas?

sawa
  • 165,429
  • 45
  • 277
  • 381
  • got same issue. cant get it to interpret ruby lines – GnrlBzik May 12 '11 at 15:34
  • actually take a look at this post: http://stackoverflow.com/questions/2752906/how-to-access-html-request-parameters-for-a-rhtml-page-served-by-webrick – GnrlBzik May 12 '11 at 15:42

1 Answers1

4

Ok figured this one out, not completely though. You need to specify mime type :MimeTypes => {'rhtml' => 'text/html'}, i copied this from http://www.ruby-forum.com/topic/96436, recognizes rhtml and runs what ever ruby code you have in <%...%>.

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)
GnrlBzik
  • 3,358
  • 5
  • 27
  • 37
  • Thank you GnrlBzik! It works using .rhtml... no way using .erb! – Matt Stifanelli May 12 '11 at 18:22
  • you welcome, yeah, i am not getting erb to work either but so far i do not need anything beyond rhtml, just to test stuff in browser, rest of the things i do are done in rails. why did you want erb in a first place? did you assume that this is what files need to be named? i am asking because i assumed that too. – GnrlBzik May 12 '11 at 18:33