Is there a framework or something out there so that I can develop webpages in Ruby the same way I can as PHP. Something like
<html><head></head><body>
<?ruby
puts '<p> Hello there!</p>'
?>
</body></html>
The only thing I'm seeing for using Ruby in webpages is huge complex frameworks that is completely different from how PHP works. I mean, sure that's all fine and dandy with the 3 tier model and such but when your just wanting a few simple things done(which are trivial in PHP) in a webpage, to setup such a large framework just doesn't seem right. Especially when you only really want like 1 page made in Ruby and the rest being plain HTML.
The default behavior for PHP is to run as CGI scripts, which means that the web server calls php-cgi <path/to/php-script>
or something similar, passing quite a lot of environment variables. To do the same with Ruby, you need to setup a script to handle .rb files. This varies wildly depending on your web server, but if you are using Apache 2.2, put this in your httpd.conf or .htaccess file:
Action ruby-cgi /path/to/ruby-cgi
AddHandler ruby-cgi .rb
# You might want to add this too:
DirectoryIndex index.rb index.html
You could either specify the path to your ruby executable (run which ruby
to get the path), or to any other script that accepts a filename as the first parameter. If you use the ruby executable, nothing magical happens, and you can't insert erb into the file without adding some ERB compiling yourself. However, you could use my ruby-cgi
script, which does several things:
$CGI
. See below for an example on how to use this.This is a simple example script on how you can use the ruby-cgi
"magic":
<% header "Content-Type" => "text/html" %>
<html>
<head>
<title><%= $CGI['title'] %>
</head>
<body>
<h1><%= $CGI['title'] %>
</body>
</html>
Let's say you put this into the webroot with the name example.rb
. If you then access this with a URL similar to http://example.com/example.rb?title=Hello%20world it should set the title to "Hello world", and it should display a <h1> with "Hello world" in it.
If you find any bugs with the script, feel free to fork the gist and update it.
Two words: Sinatra and ERB
(For lightweight sites, at least).
Sinatra is a simple HTTP server, ERB is a templating system that acts similar to templating in PHP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With