railsの勉強
ブックマークアプリを作ってみた。
DBを用意しておいて、railsコマンドでアプリケーションを作って、scaffoldしてみる。
modelとview、controllerが一緒にできて、とりあえずブックマークアプリの出来上がり。
ruby script/generate scaffold Bookmark Admin
scaffoldを使わないでやるにはどうしたらいいか気になっていたが、
とやると、モデルだけできるらしい。
ruby script/generate model Bookmark
modelであるBookmarkクラスが作られたわけ。(その他は、テスト用とDBのユーティリティのようだ。)Bookmarkクラスは、ActiveRecord::Baseをベースクラスとした空のクラス。scaffoldで作られるものと同じ。DBのあるなしに関係なくmodelを作れると思う。
handyhal:bookmark $ ruby script/generate model Bookmark
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/bookmark.rb
create test/unit/bookmark_test.rb
create test/fixtures/bookmarks.yml
create db/migrate
create db/migrate/001_create_bookmarks.rb
handyhal:bookmark $ cd app/models/
handyhal:models $ ls
bookmark.rb
handyhal:models $
scaffoldを使ったときに生成されるファイルが気になったので、もう一度やり直し。
config/database.ymlを設定しなおしてやり直し。
handyhal:bookmark $ ruby script/generate scaffold Bookmark Admin
exists app/controllers/
exists app/helpers/
create app/views/admin
exists test/functional/
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/bookmark.rb
identical test/unit/bookmark_test.rb
identical test/fixtures/bookmarks.yml
#28000Access denied for user 'root'@'localhost' (using password: NO)
handyhal:bookmark $
結局、scaffoldの実行で、以下が追加で作成された。
handyhal:bookmark $ ruby script/generate scaffold Bookmark Admin
exists app/controllers/
exists app/helpers/
exists app/views/admin
exists test/functional/
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/bookmark.rb
identical test/unit/bookmark_test.rb
identical test/fixtures/bookmarks.yml
create app/views/admin/_form.rhtml
create app/views/admin/list.rhtml
create app/views/admin/show.rhtml
create app/views/admin/new.rhtml
create app/views/admin/edit.rhtml
create app/controllers/admin_controller.rb
create test/functional/admin_controller_test.rb
create app/helpers/admin_helper.rb
create app/views/layouts/admin.rhtml
create public/stylesheets/scaffold.css
handyhal:bookmark $
- app/views/admin
- app/views/admin/_form.rhtml
- app/views/admin/list.rhtml
- app/views/admin/show.rhtml
- app/views/admin/new.rhtml
- app/views/admin/edit.rhtml
- app/controllers/admin_controller.rb
- test/functional/admin_controller_test.rb
- app/helpers/admin_helper.rb
- app/views/layouts/admin.rhtml
- public/stylesheets/scaffold.css
adminページのビューとコントローラをモデルから作ったということのようだ。
ビューだけできないかと思って、
なんてやってみたけど、view generatorは、無いらしい。でも、コントローラは作ることができて、
ruby script/generate view Admin
scaffoldの生成後なので、admin_controllerが上書きされた。
handyhal:bookmark $ ruby script/generate controller Admin
exists app/controllers/
exists app/helpers/
exists app/views/admin
exists test/functional/
overwrite app/controllers/admin_controller.rb? [Ynaq] y
force app/controllers/admin_controller.rb
overwrite test/functional/admin_controller_test.rb? [Ynaq] Y
force test/functional/admin_controller_test.rb
identical app/helpers/admin_helper.rb
handyhal:bookmark $ ruby script/generate view Admin
Couldn't find 'view' generator
handyhal:bookmark $
generate scaffoldで作られたadmin_controllerは、index, list, show, newといったメソッドが作られているが、generate controllerで作られたadmin_controllerは、ApplicationControllerをベースクラスとする空のクラス。generate scaffoldでは、DBに対する一通りの操作が用意されるようだ。