Merb + ActiveRecord + SQLite3 で簡単アプリ


前回、コントローラの作成をしたけどやっぱりデータベースを使わないと面白くない。
(Merbでの簡単な画面作成 - ナカオ日記−フリーランスの轍)


っつーことで Merb + ActiveRecord + SQLite3 でアプリ作成
(AvtiveRecord を使うのは Rails3 を勝手に見越して)

前提

>gem install activerecord

MerbとActiveRecordの連携

MerbとActiveRecordを連携させるにはmerb_activerecordっつーgemが必要

>gem install merb_activerecord
Successfully installed merb_activerecord-1.0.0.1
1 gem installed
Installing ri documentation for merb_activerecord-1.0.0.1...
Installing RDoc documentation for merb_activerecord-1.0.0.1...

設定変更

APP_ROOT\config\dependencies.rb
一番下に、以下を追記

dependency "merb_activerecord"

リソースの作成

RailsでいうところのScaffoldの作成

>cd merb_test
>merb-gen resource bookmark title:string,memo:text

カラム間のカンマ後にスペース不可。
(ちょっとだけハマったのは内緒)

テーブル作成

>rake db:automigrate
(in c:/rbc/merb_test)
Loading init file from c:/rbc/merb_test/config/init.rb
Loading c:/rbc/merb_test/config/environments/development.rb
Loading c:/rbc/merb_test/config/environments/rake.rb
~ Connecting to database...
~ Loaded slice 'MerbAuthSlicePassword' ...
~ Compiling routes...
~ Activating slice 'MerbAuthSlicePassword' ...

モデル修正

APP_ROOT\app\models\bookmark.rb
こんなカンジで、ActiveRecordを実装

class Bookmark < ActiveRecord::Base
end

サーバ起動

>merb
Loading init file from c:/rbc/merb_test/config/init.rb
Loading c:/rbc/merb_test/config/environments/development.rb
~ Connecting to database...
~ Connecting to database...
~ Loaded slice 'MerbAuthSlicePassword' ...
~ Compiling routes...
~ Disconnecting database connection before forking.
~ Activating slice 'MerbAuthSlicePassword' ...
merb : worker (port 4000) ~ Starting Mongrel at port 4000
merb : worker (port 4000) ~ Successfully bound to port 4000


http://localhost:4000/bookmarks
ブラウザで以下の画面が確認できればオッケー

画面の作りこみ

http://wiki.merbivore.com/howto/crud_view_example_with_merb_using_erb
ここにサンプルがあるので、そいつをベースに作成


APP_ROOT\app\views\bookmarks\index.html.erb

<h1>Bookmark controller, index action</h1>
 
<table>
  <tr>
    <th>Title</th>
    <th>Memo</th>
 
    <th colspan="3">Actions</th>
  </tr>
 
<% @bookmarks.each do |bookmark| %>
  <tr>
    <td><%=h bookmark.title %></td>
    <td><%=h bookmark.memo %></td>
 
    <td><%= link_to 'Show', resource(bookmark) %></td>
    <td><%= link_to 'Edit', resource(bookmark, :edit) %></td>
    <td><%= delete_button(bookmark, "Delete #{bookmark.title}") %></td>
  </tr>
<% end %>
</table>
 
<%= link_to 'New', resource(:bookmarks, :new) %>


APP_ROOT\app\views\bookmarks\new.html.erb

<h1>Bookmark controller, new action</h1>
 
<%= form_for(@bookmark, :action => resource(:bookmarks) ) do %>
 
  <p>
    <%= text_field :title, :label => "Title " %>
  </p>
 
  <p>
    <%= text_area :memo, :label => "Memo "  %>
  </p>
 
  <p>
    <%= submit "Create" %>
  </p>
<% end =%>
 
<%= link_to 'Back', resource(:bookmarks) %>


APP_ROOT\app\views\bookmarks\edit.html.erb

<h1>Bookmark controller, edit action</h1>
 
 
<%= form_for(@bookmark, :action => resource(@bookmark)) do %>
 
  <p>
    <%= text_field :title, :label => "Title"  %>
  </p>
 
  <p>
    <%= text_area :memo, :label => "Memo"  %>
  </p>
 
  <p>
    <%= submit "Update" %>
  </p>
<% end =%>
 
<%= link_to 'Show', resource(@bookmark) %> | <%= link_to 'Back', resource(:bookmarks) %>


APP_ROOT\app\views\bookmarks\show.html.erb

<h1>Bookmark controller, show action</h1>
 
<h3><%=h @bookmark.title %></h3>
 
<p>
  <%=h @bookmark.memo %>
</p>
 
<%= link_to 'Back', resource(:bookmarks) %>


あと、ActiveRecordに変更したのでコントローラも一部修正
APP_ROOT\app\controllers\bookmarks.rb
以下のメソッド内の記述を修正

  • show
  • delete
  • update
  • destroy

@bookmark = Bookmark.find(id)

get → find に修正


これでオッケー

サンプル

2009-05-14 - h-nakaoの日記 - hiroshinakaoグループ
こちらにzipを配置