> つぎは、DBテーブルを修正して、リンク数を記録できるようにしたい。
まず、DB定義。


drop table if exists bookmarks;
create table bookmarks (
id int not null auto_increment,
col_time timestamp not null,
title varchar(100) not null,
url text not null,
description text not null,
primary key (id)
);
となっていたのを修正。ref_countカラムを追加し、ついでにtitleをvarchar(100)からtextに変更。文字列のカラムがすべてtextなのは、いまいちか。
drop table if exists bookmarks;
create table bookmarks (
id int not null auto_increment,
col_time timestamp not null,
title text not null,
url text not null,
description text not null,
ref_count int not null,
primary key (id)
);
テーブルを作り直す前に、mysqldumpで現在のブックマークのバックアップをとり、テーブルを作り直して、テーブルをリストア。
WEBrickを起動しなおして、ブックマークを開いて、動いていることを確認。
アンカーをクリックしたときに参照数を増やすのは、admin_controller.rbのredirect_hookメソッド。ちょっとだけの修正。
  def redirect_hook
@bookmark = Bookmark.find(params[:id])
# このURLの参照カウントを増やす。(DBに反映)
@bookmark.ref_count += 1
@bookmark.update_attributes(params[:bookmark])
redirect_to @bookmark.url
end
デバッグの意味も含めて、ビュー(list.rhtml)に参照数を表示するように変更。タイトルを表示するカラムの前に参照数を表示するカラムを追加。
    <%= bookmark.ref_count %>
適当なアンカーをクリックして、コンテンツを表示後、list.rhtmlを再表示させて、参照数がインクリメントされていることを確認。