LoginSignup
23
23

More than 5 years have passed since last update.

Ruby on Rails コマンド覚書

Last updated at Posted at 2014-03-23

アプリケーション作成時

プロジェクトディレクトリ作成

$ mkdir project1
$ cd project1

Railsアプリケーション作成

$ rails new application1
$ cd application1
# デフォルトのテストを使わないならば-Tオプションを付ける
$ rails new application1 -T

Gemfile編集

$ vi Gemfile
# gem 'gem name', 'version'
gem 'rails', '4.0.4'
gem 'sqlite3'

...

gemのインストール

# systemのほうにインストールする場合
$ bundle install
# プロジェクト配下にインストールする場合
$ bundle install --path vendor/bundle

Model作成時

データベースの設定

$ vi config/database.yml
database.yml
development:
    adapter: sqlite3
    database: db/development.sqlite3
    pool: 5
    timeout: 5000

test:
    adapter: sqlite3
    database: db/test.sqlite3
    ...

production:
    adapter: sqlite3
    database: db/production.sqlite3
    ...

データベースの作成

$ rake db:create

Modelファイル作成

$ rails g model Person name age:integer sex:boolean

その後以下ファイルを直接編集しても良い。

$ vi db/migrate/xxxxxxxx_create_xxxxxx.rb

生成済みマイグレーションファイルにインデックス追加

$ rails g migration add_index_to_<モデル名s>_<カラム>

DBマイグレーション

$ rake db:migrate
# 間違った場合はリセット
$ rake db:migrate:reset

テスト用DB作成

$ rake db:test:prepare

Controller作成時

Controllerファイル作成

# rails g controller コントローラー名 [アクション名]
$ rails g controller Projects index

Routes設定

$ rake routes

実行時

サーバー起動

# 通常起動
$ rails s
# デーモンとして起動
$ rails s --d
# 本番として起動
$ rails s -e production

railsコンソール起動

# 変更が反映されないサンドボックス状態でコンソール起動
$ rails c --sandbox
# 変更が反映されるモードでコンソール起動
$ rails c

その他

注釈コメント挿入

# annotateを導入する
$ gem i annotate
# モデル変更のたびに実行し直す
$ bundle exec annotate

正規表現作成

以下のサイトで確かめてから作る

Rubular

23
23
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
23
23