TodoList: 公開情報の表示2006年12月20日 11時37分38秒

データベースの準備が済んだところで、ソースコードに変更を加える。まずは、ruby script/generate scaffold で作った、todo_contgroller.rb の show 関数を変更する。

% cvs diff app/controllers/todo_controller.rb
diff -u -r1.4 todo_controller.rb
--- app/controllers/todo_controller.rb  15 Dec 2006 05:06:26 -0000      1.4
+++ app/controllers/todo_controller.rb  16 Dec 2006 22:22:24 -0000
@@ -14,6 +14,12 @@
 
   def show
     @todo = Todo.find(params[:id])
+    @organizations = Organization.find_all()
+    @published_organizations =
+      TodoOrganizationPermission.find(:all,
+       :conditions=>["todo_id=?", params[:id]])
+      #TodoOrganizationPermission.find_all("todo_id = " + params[:id])
+    @organization_ids = @published_organizations.map { |o| o.organization_id }
   end
 
   def new

好奇心から find() を使ってみた。find_all() でも構わない。

次に、これまた scaffold で生成されたapp/views/todo/show.rhtml を編集する。なお、以下のコードにはデバッグ用のコードが残っている。


<% for column in Todo.content_columns %>
<p>
  <b><%= column.human_name %>:</b> <%=h @todo.send(column.name) %>
</p>
<% end %>

<table>
  <tr>
  <% for column in Organization.content_columns %>
    <th><%= column.human_name %></th>
  <% end %>
   <th>Published-to</th>
  </tr>
  
<% for org in @organizations %>
  <tr>
  <% for column in Organization.content_columns %>
    <td><%= org.send(column.name) %></td>
  <% end %>
    <td><%= if @organization_ids.member?(org.id) then "YES"
      else "NO" end %></td>
    <td><%=
      if @organization_ids.member?(org.id) &&
        @published_organizations[@organization_ids.index(org.id)]['published'] ! = 0
      then
    "YES"
      else
    "NO"
      end %></td>
  </tr>
<% end %>
</table>

<%= link_to 'Edit', :action => 'edit', :id => @todo %> |
<%= link_to 'Back', :action => 'list' %>

<p>

<%= @published_organizations.inspect %>

UYOTA

<%= @organization_ids.inspect %>

結果だけを見ると簡単だが、色々と試行錯誤しながらすすめたので、時間がかかった。

前回次回