Railsを少し触ってみてちょっと機能が多すぎるなぁってことでSinatraでアプリを作ってみて、後からまたRailsでやってみようと思っていましたが、なぜか逆走して今回はErbのみを使ってRubyでHtmlをやってみたいと思います。
Apacheの設定はCGIを許可することは必須ですが、ユーザー単位でCGIの許可と.htaccessの更新を許可した方が楽かもしれません。
 そうしておくと、index.rhtmlをドキュメントルートで読むようにする事だったり、ユーザーでインストールしたrbenvで管理しているバージョンのRubyでの実行も可能になります。
 以下で動くと思いますが、ありあり設定なのでセキュリティは個々で変える必要ありあり。
<IfModule mod_userdir.c> # UserDir disabled UserDir public_html </IfModule> <Directory /home/*/public_html> AllowOverride All Options Includes ExecCGI FollowSymLinks <Limit GET POST OPTIONS> Order allow,deny Allow from all </Limit> <LimitExcept GET POST OPTIONS> Order deny,allow Deny from all </LimitExcept> </Directory>
.htaccess DirectoryIndex index.html index.rhtml AddHandler rubypage .rhtml Action rubypage /erb.cgi
erb.cgi はこちらから
Serving .rhtml Files on OS X With Apache and ERB – dekstop weblog:
ありがたく利用しますが、このままだとDebug用に設置されているApacheの情報がまるっと出ちゃいますので少しだけ弄くります。
diff -u
--- public_html/erb.cgi.org    2015-01-04 14:53:31.936164210 +0900
+++ public_html/erb.cgi    2015-01-04 14:53:57.551158992 +0900
@@ -1,4 +1,6 @@
-#!/usr/local/bin/ruby
+#!/home/takeken/.rbenb/shims/ruby
+
+print "Content-Type: text/html\n\n";
 
 # erb.cgi
 #
@@ -9,11 +11,14 @@
 # Martin Dittus (martin@dekstop.de), 2006-01-09
 # last change: 2006-01-11
 
+# 20150104 takeken little change
+
 require 'time'
 require 'erb'
 
 time = Time.now.httpdate
 
+
 HEADERS = <<EOF
 Date: #{ time }
 Server: #{ ENV['SERVER_SOFTWARE'] }
@@ -33,7 +38,8 @@
     raise "Attempt to access invalid path: #{path}" unless path.index(ENV['DOCUMENT_ROOT']) == 0
   end
   erb = File.open(path) { |f| ERB.new(f.read) }
-  print HEADERS + erb.result(binding)
+#  print HEADERS + erb.result(binding)
+  print erb.result(binding)
   
 rescue Exception
   
@@ -44,13 +50,13 @@
   print "<pre>#{ $! }</pre>"
 
   # debug info
-  print "<h2>Backtrace</h2>"
-  print "<pre>#{$!.backtrace.join("\n")}</pre>"
+#  print "<h2>Backtrace</h2>"
+#  print "<pre>#{$!.backtrace.join("\n")}</pre>"
 
-  print "<h2>Environment</h2>"
-  print "<pre>#{ENV.keys.map { |key| key + ' = ' + ENV[key] + "\n"} }</pre>"
+#  print "<h2>Environment</h2>"
+#  print "<pre>#{ENV.keys.map { |key| key + ' = ' + ENV[key] + "\n"} }</pre>"
 
-  print "<hr>"
-  print "<i>#{__FILE__} -- #{time}</i>"
+#  print "<hr>"
+#  print "<i>#{__FILE__} -- #{time}</i>"
   
 end
これで土台はできました。
[takeken@ubuntu]$ cat public_html/test.rhtml
<html>
<head>
</head>
<body>
<h2>たけけんのRuby勉強日記</h2>
<h6>ご利用は自己責任で(^_^)v</h6>
<h3><b>index.rhtml + erb.cgi を使って ruby で html</b></h3>
<p>ただいま<%=Time.now%>でございます。</p>
ループの開始
<% %w{One Two Three}.each do |x| %>
  <%=    x %> 回目
<% end %> 
<% yen = 2980 * 1.05 %>
<p>
本の値段は<%= yen %>円です。
</p>
</body>
</html>
こんな感じのindexファイルを作ってみますた。ブラウザから見るとこうなります。
はじめはヒアドキュメントを併用してCGIとRUBYでやっていたのですが、もっと良い方法はないものかと探していて見つけました。
参考サイト
START HERE ruby埋め込みスクリプトrhtml on windows:
