sinatraでcoffee-scriptをはじめてみる
準備
あらかじめ、Node.jsをインストールしておいてください。
% gem install sinatra coffee-script
ソースコード
必要最小限のコード量になっています。
- hello.rb
require 'sinatra' require 'coffee-script' get '/hello.js' do coffee :hello end get '/' do '<html><body><script type="text/javascript" src="./hello.js"></script></body></html>' end
- views/hello.coffee
document.write "Hi, coffee"
実行
% ruby hello.rb [2013-02-27 05:31:59] INFO WEBrick 1.3.1 [2013-02-27 05:31:59] INFO ruby 1.9.3 (2013-01-15) [x86_64-linux] == Sinatra/1.3.5 has taken the stage on 4567 for development with backup from WEBrick [2013-02-27 05:31:59] INFO WEBrick::HTTPServer#start: pid=34279 port=4567
ブラウザから、http://localhost:4567/ にアクセスして、"Hi, coffee"を確認できればおーけー。
メモ
今回、Node.jsをあらかじめ必要とした構成ですが、Asset Pipelineというものを使うとNode.jsをインストールしなくても済むかもしれません。