レイルに乗ってみた〜 目次
書籍『Rails of Ruby on Rails ~Case of LOCUSANDWONDERS.COM~』で勉強してみた(p093〜)
コメント機能 ベースを生成
スキャッフォールドジェネレータでコメント機能のベースを自動生成
(途中scaffold.cssを上書きするか訊いてくるからNOで。overwrite public/stylesheets/scaffold.css? (enter "h" for help) [Ynaqdh] n)
~/work/locus $ script/generate scaffold Comment name:string email:string url:string content:text entry:references
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/comments
exists app/views/layouts/
exists test/functional/
exists test/unit/
exists public/stylesheets/
create app/views/comments/index.html.erb
create app/views/comments/show.html.erb
create app/views/comments/new.html.erb
create app/views/comments/edit.html.erb
create app/views/layouts/comments.html.erb
overwrite public/stylesheets/scaffold.css? (enter "h" for help) [Ynaqdh] n
skip public/stylesheets/scaffold.css
create app/controllers/comments_controller.rb
create test/functional/comments_controller_test.rb
create app/helpers/comments_helper.rb
route map.resources :comments
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/comment.rb
create test/unit/comment_test.rb
create test/fixtures/comments.yml
exists db/migrate
create db/migrate/20080910014516_create_comments.rb
+Loaded suite script/generate
Started
Finished in 0.000414 seconds.
0 tests, 0 assertions, 0 failures, 0 errors
マイグレーションファイルを修正
db/migrate/20080910014516_create_comments.rbに
def self.upに追加(書籍と違いファイル名が「修正日+_create_comments.rb」になる。なぜ?)
add_column :entries, :comments_count, :integer, :default => 0
add_index :comments, :entry_id
def self.downに追加
remove_column :entries, :comments_count
remove_index :comments, :entry_id
マイグレーション実行
~/work/locus $ rake db:migrate
(in /Users/username/work/locus)
== 20080910014516 CreateComments: migrating ===================================
-- create_table(:comments)
-> 0.0269s
-- add_column(:entries, :comments_count, :integer, {:default=>0})
-> 0.0212s
-- add_index(:comments, :entry_id)
-> 0.0579s
== 20080910014516 CreateComments: migrated (0.1127s) ==========================
Loaded suite /opt/local/bin/rake
Started
Finished in 0.00045 seconds.
0 tests, 0 assertions, 0 failures, 0 errors
モデルの修正 モデル間の関連付け
app/models/comment.rbに追加
belongs_to :entry, :counter_cache => true
app/models/entry.rbに追加
has_many :comments, :order => "created_at ASC"
バリデーションを追加
app/models/comment.rbに追加
validates_presence_of :name, :email, :content validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
ネステッドルート
config/routes.rbを修正
「map.resources ::trackbacks」を削除とあるけど、もともと書いて無かった。見落とした?
#map.resources :entries map.resources :entries, :has_many => [:comments, :trackbacks]
このtrackbacksって次のトラックバック機能で追加するのでは?(書籍の間違い?)
コントローラの修正 親データを常にロードする
app/controllers/comments_controller.rbに追加と置換
before_filter :load_entry before_filter :login_required, :except => [:index, :show, :new, :create] : format.html { redirect_to(@entry) } : private def load_entry @entry = Entry.find(params[:entry_id]) end
置換
- 「Comment.」を「@entry.comments.」に置換する(親データに依存するコメントをロード)
- 「redirect_to(@comment)」を「redirect_to(@entry)」に置換する(リダイレクト先を親データの記事頁に変更)
バリデーションエラー時の出力チテンプレートを変更
またapp/controllers/comments_controller.rbに修正
#format.html { render :action => "new" } format.html { render :template => "entries/show" }
コメント機能を記事に書き込む 記事毎のコメント数を表示
間違い箇所見つけた
p098に書いてあるファイル名(index.thml.erb)が間違ってるようなので、サポートサイトから落としたのサンプル中身を書籍に載ってるコード「 entry_path(entry)」でマルチファイル検索してみた。(Emacsのgrepで)
M-x grep-find Run find (like this): find . -type f -exec grep -nH -e " entry_path(entry)" {} /dev/null \;
検索結果
./locus/app/views/entries/_entry.html.erb:2: <%= link_to h(entry.title), entry_path(entry) %> ./locus/app/views/entries/_entry.html.erb:14: entry_path(entry) + '#comments') %> | ./locus/app/views/entries/_entry.html.erb:16: entry_path(entry) + '#trackbacks' %> | ./locus/app/views/entries/_entry.html.erb:23: <%= link_to '編集', edit_entry_path(entry) %>
_entry.html.erbを修正するみたい。
app/views/entries/_entry.html.erbに追加する部分
<dd> <%= link_to( "Comments (#{entry.comments_count})", entry_path(entry) + '#comments') %> | </dd>
記事にコメント表示
app/views/entries/show.html.erbに追加
サンプルの
<dd id="comments">〜 </dd>
を間に入れる
メモ
インストールとか無ければ躓くとこはあまりなさそう。


