Question

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 ..

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top