この世の言語っていうのは構文をわざと難しくしてるんじゃねーのか問題
英語には抵抗ない。
ただこれはもうね、英語がどうのこうのの問題ではないと思うわ。
特にrails generate。
プログラミングのハードルをあげてる呪文選手権ぶっちぎり1位(俺調べ)。
rails generate呪文すぎワロエナイ
rails generateふざっけんな!(とりまファビョる)
いったん香ばしいにわか臭を放つためにrails generate -h
をしてみる。
$ rails generate -h Running via Spring preloader in process 4775 Usage: rails generate GENERATOR [args] [options] General options: -h, [--help] # Print generator's options and usage -p, [--pretend] # Run but do not make any changes -f, [--force] # Overwrite files that already exist -s, [--skip] # Skip files that already exist -q, [--quiet] # Suppress status output Please choose a generator below. Rails: assets channel controller generator helper integration_test jbuilder job mailer migration model resource scaffold scaffold_controller system_test task Coffee: coffee:assets Js: js:assets TestUnit: test_unit:generator test_unit:plugin
は?(英語の単語を調べる音)
なんとなくここが使い方っぽいぞ。
Usage: rails generate GENERATOR [args] [options]
で、Please choose a generator below.に上のGENERATOR部分に入る文字列が指定されてるからこれを指定して-hしてみる。
今回はcontrollerを指定して実行。
$ rails generate controller -h Running via Spring preloader in process 4800 Usage: rails generate controller NAME [action action] [options] Options: [--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications) [--skip-routes], [--no-skip-routes] # Don't add routes to config/routes.rb. [--helper], [--no-helper] # Indicates when to generate helper # Default: true [--assets], [--no-assets] # Indicates when to generate assets # Default: true -e, [--template-engine=NAME] # Template engine to be invoked # Default: erb -t, [--test-framework=NAME] # Test framework to be invoked # Default: test_unit Asset options: -j, [--javascripts], [--no-javascripts] # Generate JavaScripts # Default: true -y, [--stylesheets], [--no-stylesheets] # Generate Stylesheets # Default: true -je, [--javascript-engine=JAVASCRIPT_ENGINE] # Engine for JavaScripts # Default: coffee -se, [--stylesheet-engine=STYLESHEET_ENGINE] # Engine for Stylesheets # Default: scss Runtime options: -f, [--force] # Overwrite files that already exist -p, [--pretend], [--no-pretend] # Run but do not make any changes -q, [--quiet], [--no-quiet] # Suppress status output -s, [--skip], [--no-skip] # Skip files that already exist Description: Stubs out a new controller and its views. Pass the controller name, either CamelCased or under_scored, and a list of views as arguments. To create a controller within a module, specify the controller name as a path like 'parent_module/controller_name'. This generates a controller class in app/controllers and invokes helper, template engine, assets, and test framework generators. Example: `rails generate controller CreditCards open debit credit close` CreditCards controller with URLs like /credit_cards/debit. Controller: app/controllers/credit_cards_controller.rb Test: test/controllers/credit_cards_controller_test.rb Views: app/views/credit_cards/debit.html.erb [...] Helper: app/helpers/credit_cards_helper.rb
ふぁーw
NAME ってなに?
[action action]ってなーに!(キレ気味)
調べるとNAMEにはリソースの頭文字が大文字のものが入って、actionにはそのリソースに対するアクションを入れるらしい。
アクションというのは、controllerのメソッドのことらしく、例えばnewというアクションはそのリソースのクラスをインスタンス化させるメソッドらしい。
では今度はmodelでやってみっぞ。
$ rails generate model -h Running via Spring preloader in process 4871 Usage: rails generate model NAME [field[:type][:index] field[:type][:index]] [options] Options: [--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications) [--force-plural], [--no-force-plural] # Forces the use of the given model name -o, --orm=NAME # ORM to be invoked # Default: active_record ActiveRecord options: [--migration], [--no-migration] # Indicates when to generate migration # Default: true [--timestamps], [--no-timestamps] # Indicates when to generate timestamps # Default: true [--parent=PARENT] # The parent class for the generated model [--indexes], [--no-indexes] # Add indexes for references and belongs_to columns # Default: true [--primary-key-type=PRIMARY_KEY_TYPE] # The type for primary key -t, [--test-framework=NAME] # Test framework to be invoked # Default: test_unit TestUnit options: [--fixture], [--no-fixture] # Indicates when to generate fixture # Default: true -r, [--fixture-replacement=NAME] # Fixture replacement to be invoked Runtime options: -f, [--force] # Overwrite files that already exist -p, [--pretend], [--no-pretend] # Run but do not make any changes -q, [--quiet], [--no-quiet] # Suppress status output -s, [--skip], [--no-skip] # Skip files that already exist Description: Stubs out a new model. Pass the model name, either CamelCased or under_scored, and an optional list of attribute pairs as arguments. Attribute pairs are field:type arguments specifying the model's attributes. Timestamps are added by default, so you don't have to specify them by hand as 'created_at:datetime updated_at:datetime'. As a special case, specifying 'password:digest' will generate a password_digest field of string type, and configure your generated model and tests for use with Active Model has_secure_password (assuming the default ORM and test framework are being used). You don't have to think up every attribute up front, but it helps to sketch out a few so you can start working with the model immediately. This generator invokes your configured ORM and test framework, which defaults to Active Record and TestUnit. Finally, if --parent option is given, it's used as superclass of the created model. This allows you create Single Table Inheritance models. If you pass a namespaced model name (e.g. admin/account or Admin::Account) then the generator will create a module with a table_name_prefix method to prefix the model's table name with the module name (e.g. admin_accounts) Available field types: Just after the field name you can specify a type like text or boolean. It will generate the column with the associated SQL type. For instance: `rails generate model post title:string body:text` will generate a title column with a varchar type and a body column with a text type. If no type is specified the string type will be used by default. You can use the following types: integer primary_key decimal float boolean binary string text date time datetime You can also consider `references` as a kind of type. For instance, if you run: `rails generate model photo title:string album:references` It will generate an `album_id` column. You should generate these kinds of fields when you will use a `belongs_to` association, for instance. `references` also supports polymorphism, you can enable polymorphism like this: `rails generate model product supplier:references{polymorphic}` For integer, string, text and binary fields, an integer in curly braces will be set as the limit: `rails generate model user pseudo:string{30}` For decimal, two integers separated by a comma in curly braces will be used for precision and scale: `rails generate model product 'price:decimal{10,2}'` You can add a `:uniq` or `:index` suffix for unique or standard indexes respectively: `rails generate model user pseudo:string:uniq` `rails generate model user pseudo:string:index` You can combine any single curly brace option with the index options: `rails generate model user username:string{30}:uniq` `rails generate model product supplier:references{polymorphic}:index` If you require a `password_digest` string column for use with has_secure_password, you can specify `password:digest`: `rails generate model user password:digest` If you require a `token` string column for use with has_secure_token, you can specify `auth_token:token`: `rails generate model user auth_token:token` Examples: `rails generate model account` For Active Record and TestUnit it creates: Model: app/models/account.rb Test: test/models/account_test.rb Fixtures: test/fixtures/accounts.yml Migration: db/migrate/XXX_create_accounts.rb `rails generate model post title:string body:text published:boolean` Creates a Post model with a string title, text body, and published flag. `rails generate model admin/account` For Active Record and TestUnit it creates: Module: app/models/admin.rb Model: app/models/admin/account.rb Test: test/models/admin/account_test.rb Fixtures: test/fixtures/admin/accounts.yml Migration: db/migrate/XXX_create_admin_accounts.rb
ヘルプが長すぎ問題。
まるで海で溺れている泳げない俺に執拗に泳ぎ方を教えてくるインストラクターのようだ。
まぁ使い方を知りたいときは-hはみるなということで理解したわ。
知りたいのはどの呪文をすると、どんなモンスターが生まれるのかということ
で実際のところ知りたいのは、
rails generate controllerでどんなファイルが作られて、
それがmodelやresource、assetsだとどうなるのかなんだな。
(てかassetsだけ複数なんだな。)
コマンド名 | コントローラ | ビュー | モデル | マイグレーション | アセット | ルート | テスト | ヘルパー |
---|---|---|---|---|---|---|---|---|
scaffold | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ |
scaffold_controller | ○ | ○ | × | × | × | × | ○ | ○ |
controller | ○ | ○ | × | × | ○ | ○ | ○ | ○ |
model | × | × | ○ | ○ | × | × | ○ | × |
migration | × | × | × | ○ | × | × | ○ | × |
テーブルで図示するとこんな感じになる。
つまりだ。
controllerは同時にviewを作成して、
modelは同時にmigrationを作成してくれるんだな。
で、testはどのジェネレータ使っても作成される、と。
とりあえずここまで覚えればいいか。(にわかの鑑だろ?)
んなわけない
でここからはアクションだな。
controllerとscaffold_controllerという似たようなジェネレータがあるんだけど、違いは以下だ。
- controllerはジェネレータの後ろにコントローラ名を指定して、アクションにメソッド名を指定する
- scaffold_controllerはジェネレータの後ろにモデル名を指定して、その後ろにカラム名:型を指定する
これクソわかりづらくないか?
scaffold_controllerというジェネレータの後ろにmodel名だと!?
controllerなのかmodelなのかわからんわ!
しかもUsageみると
Usage: rails generate scaffold_controller NAME [field:type field:type] [options]
と書いてあるが、modelは生成されないという、、、
謎仕様やめろ(切実)。
以下例
Example: `rails generate scaffold_controller CreditCard` Credit card controller with URLs like /credit_card/debit. Controller: app/controllers/credit_cards_controller.rb Test: test/controllers/credit_cards_controller_test.rb Views: app/views/credit_cards/index.html.erb [...] Helper: app/helpers/credit_cards_helper.rb
とりあえずscaffold_controllerの場合のジェネレータは頭文字大文字単数形で書くことにするお。
そしてカラム名:型は指定しないぞ(決意)。
あと違いは
- scaffold_controllerはRESTfulなメソッドを全て作成する
- controllerはアクションに指定したメソッドのみ作成する
(generate scaffold系は)ルーティングに resources :users が追加され、アプリケーションにおける基本的な機能である一覧(index)、詳細(show)、新規作成(new/create)、編集(edit/update)、削除(destroy) を実現するために必要なファイルが追加されます。
ということかな。
これはscaffold作成したことある人なら理解できると思うは。(作成しないとわからない)
まとめ
まとまらない(確信)。
とりあえず表を丸暗記だよっ!(単純)