プラグイン追加
以下のコマンドで既存のプロジェクトにcoffeescriptプラグインを追加。
$ padrino g plugin coffee
Gemfile
とapp/app.rb
に必要なgemや設定が追記され、lib/coffee_initializer.rb
が作成される。
gemインストール
Gemfileに追記されたgemをインストール。
$ bundle install
※ therubyracer
がインストールされていない場合は、Gemfileに追記してからbundle install
coffeescript追加
public/javascripts/
以下に.coffeeファイルを追加。
public/javascripts/hello.coffee
console.log 'Hello coffee'
viewから呼び出し
通常のjavascript呼び出しと同様に、javascript_include_tag
でok。
hoge.slim
= javascript_include_tag 'top'
※ 例ではテンプレートエンジンにslimを使っている