LoginSignup
3
1

More than 5 years have passed since last update.

Rails Application Templates 設定の自動化

Last updated at Posted at 2017-09-04

rails application templateとは

例えば開発初期のgemを入れて、設定を書き込んでという作業を1ファイルで自動化してくれるrailsの標準機能です。ちょっとしたミスの混入を防ぎ、またgithub上のテンプレートであれば0秒で利用できます。

rails application templateの日本語訳がなかったので自分の為も含めて翻訳しました。自信がないところは英文ものせています。間違って翻訳していたらコメントください。
ぜひテンプレートを使って楽しいruby lifeを送りましょう。

翻訳した後に気づきましたが以下に翻訳されていました。以下の記事のが詳しいので基本的に以下の記事を参照してください。
https://railsguides.jp/rails_application_templates.html
ただこの記事では便利な書き方を載せていないので補足します。

Rails Application Templates(Rails Guidesの翻訳)

http://guides.rubyonrails.org/rails_application_templates.html
Application templates は新規のRailsプロジェクトまたは作成済みのRailsプロジェクトにgems/initializersを加えるためのDSLで書かれたシンプルなRubyファイルのことです。

1 使用法

templateを適用するにはrails g コマンドに-mオプションを使ってtemplateの場所を指定します。この時のテンプレートの場所はパス、URLどちらでも大丈夫です。

$ rails new blog -m ~/template.rb
$ rails new blog -m http://example.com/template.rb

作成済みのrailsアプリにもRake taskを使うことでtemplateを使うことができます。

この時のテンプレートの場所はファイル、URLどちらでも大丈夫です。

$ bin/rails app:template LOCATION=~/template.rb
$ bin/rails app:template LOCATION=http://example.com/template.rb

2 Template API

railsのテンプレートAPIは理解するのが簡単です。以下は典型的な例です。

/template.rb
generate(:scaffold, "person name:string")
route "root to: 'people#index'"
rails_command("db:migrate")

after_bundle do
  git :init
  git add: "."
  git commit: %Q{ -m 'Initial commit' }
end

私の環境rails5.1.xでは以下のコード働きません。
generate(:scaffold, "person name:string")

run
コマンドを実行する。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1