How to execute a html embedded ruby code on local machine

1.8k views Asked by At

I am pretty much new to ruby. How can we run a html embedded ruby code on our local machine.? I have ruby 1.8 installed on my machine. Do I need to install rails too for running that html with ruby code ? I got stucked ..

2

There are 2 answers

3
Stefan On BEST ANSWER

To evaluate the Ruby code manually you can use the erb command.

Given a index.html.rhtml file with the following content:

<html><body>Hello <%= "from Ruby" %></body></html>

Running:

$ erb index.html.rhtml > index.html

Results in a index.html file with:

<html><body>Hello from Ruby</body></html>

This file can be viewed in a browser.

Another option is to use a web server to convert and serve the files. Here's a one liner starting WebBrick on port 3000 (assuming a index.html.rhtml file in the current directory):

$ ruby -rwebrick -e "WEBrick::HTTPServer.new(:DocumentRoot => '.', :Port => 3000).start"

Pointing your browser to http://localhost:3000/index.html.rhtml should show the page.

1
Nishan Hitang On