rspec_nested_scaffoldでネストした構造のscaffoldが一発生成

例えばブログ記事(Entry)の下にコメント(Comment)が複数ぶらさがっているとしますよね。
そいつを操作するプログラムを作る場合、routes.rbの指定は

map.resources :entrys do |entry|
 entry.resources :comments
end

とか指定するわけですよね。
でURLは「/entrys/1/comments」とかなるわけですよね。

こういう「URLをネストさせて」という構造、実は今まで嫌いでした。なんかメンドくさかったんですよね。ごちゃごちゃしてて。
でも「rspec_nested_scaffold」というのを知って、目から鱗が落ちましたね。「私がわるうございましたー!」みたいな。

script/plugin install git://github.com/phorsfall/rspec_on_rails_nested_scaffold.git
script/generate scaffold entry subject subject:string body:text
script/generate rspec_nested_scaffold comment body:text entry_id:integer --owner=entry

と実行して

config/routes.rb

map.resources :entries do |entries|
 entries.resources :comments
end

app/models/entry.rb

has_many :comments, :uniq => true

app/models/comment.rb

belongs_to :entry

app/views/entries/show.html.erb

%= link_to 'Show Comments', entry_comments_path(@entry) %> |

と追記すればOK! これで記事の下にコメントがついて、という構造を正しく処理してくれるプログラムが出来上がりです。

超カンタン!