1

架构:

  • persons (id, name, birthyear, gender)

  • pets (id, person_id, name, leg_count)

  • plants (id, person_id, kind, qty)

我想对这些按人分组的事情进行只读报告。人员列表已完成(没有相关记录)。我想每个人都有“子表”。就像是:

Persons
+----+------+-----------+--------+
| id | name | birthyear | gender |
+----+------+-----------+--------+
|  1 | Joe  | 1980      | M      |
+----+------+-----------+--------+
| Pets                           |
| +----+------+-----------+      |
| | id | name | Leg count |      |
| +----+------+-----------+      |
| |  1 | Rex  |         4 |      |
| +----+------+-----------+      |
| |  2 | Ka   |         0 |      |
| +----+------+-----------+      |
| Plants                         |
| +----+------------+-----+      |
| | id | kind       | qty |      |
| +----+------------+-----+      |
| |  1 | lemon tree |   2 |      |
| +----+------------+-----+      |
+----+------+-----------+--------+
|  2 | Jane | 1982      | F      |
+----+------+-----------+--------+
| Pets                           |
| +----+------+-----------+      |
| | id | name | Leg count |      |
| +----+------+-----------+      |
| |  3 | Sue  |         6 |      |
| +----+------+-----------+      |
| Plants                         |
| +----+------------+-----+      |
| | id | kind       | qty |      |
| +----+------------+-----+      |
| |  2 | Oak tree   |   1 |      |
| +----+------------+-----+      |
+----+------+-----------+--------+

你能帮我一些提示在哪里以及如何连接到框架吗?(JRuby (1.5.0)、Ruby on Rails (2.3.4)、ActiveRecord (2.3.4))

做了什么

Persons
+----+------+-----------+--------+
| id | name | birthyear | gender |
+----+------+-----------+--------+
|  1 | Joe  | 1980      | M      |
+----+------+-----------+--------+
|  2 | Jane | 1982      | F      |
+----+------+-----------+--------+

它是使用以下方法完成的:

class PersonsReportController < PersonsController
  layout 'simpreport'
  active_scaffold :person do |config|
    c.columns = [:name, :birthyear, :gender, :pets, :plants ]
    c.label = "Report of people and other living creatures"
    [:pets, :plants].each do |col| 
        columns[col].clear_link
    end
    c.list.columns.exclude :pets, :plants
    c.actions.exclude :show
  end
  # ...
end

而且我还定制了_list_header.rhtml, _list_column_headings.rhtml, 和_list_actions.rhtml一点点以消除所有交互性(如排序等)。

4

2 回答 2

5

我不明白你在控制器中编写的代码,也不明白它为什么从你的 PersonsController 继承。也许您可以多解释一下您要在那里完成的工作?

话虽这么说,我会这样解决你的问题:

楷模:

人.rb:

class Person < ActiveRecord::Base
  has_many :pets
  has_many :plants

  def has_pets?
    self.pets.size > 0
  end

  def has_plants?
    self.plants.size > 0
  end

  def has_pets_or_plants?
    self.has_pets? || self.has_plants?
  end
end

宠物.rb:

class Pet < ActiveRecord::Base
  belongs_to :person
end

植物.rb:

class Plant < ActiveRecord::Base
  belongs_to :person
end

控制器:

报告控制器.rb:

class ReportsController < ApplicationController
  def index
    @persons = Person.find(:all)
  end
end

看法:

报告/index.html.erb:

Persons
<table border="1">
  <tr>
    <td>id</td>
    <td>name</td>
    <td>birthyear</td>
    <td>gender</td>
  </tr>
<% @persons.each do |person| -%>
  <tr>
    <td><%= person.id %></td>
    <td><%= person.name %></td>
    <td><%= person.birthyear %></td>
    <td><%= person.gender %></td>
  </tr>
<% if person.has_pets_or_plants? -%>
  <tr>
    <td colspan="4">
    <% if person.has_pets? -%>
      Pets
      <table border="1">
        <tr>
          <td>id</td>
          <td>name</td>
          <td>leg count</td>
        </tr>
      <% person.pets.each do |pet| -%>
        <tr>
          <td><%= pet.id %></td>
          <td><%= pet.name %></td>
          <td><%= pet.leg_count %></td>
        </tr>
      <% end -%>
      </table>
    <% end -%>
    <% if person.has_plants? -%>
      Plants
      <table border="1">
        <tr>
          <td>id</td>
          <td>kind</td>
          <td>qty</td>
        </tr>
      <% person.plants.each do |plant| -%>
        <tr>
          <td><%= plant.id %></td>
          <td><%= plant.kind %></td>
          <td><%= plant.qty %></td>
        </tr>
      <% end -%>
      </table>
    <% end -%>
    </td>
  </tr>
<% end -%>
<% end -%>
</table>
于 2010-09-15T14:28:58.407 回答
2

这是一个适用于 ActiveScaffold 的解决方案:

应用程序/控制器/people_controller.rb

class PeopleController < ApplicationController
  active_scaffold :person do |config|
    config.label = "Report of people and other living creatures"
    config.actions.exclude :show, :delete, :edit
    # this sets up clickable links for pets and plants.
    # clicking either link will expand BOTH child object collections.
    config.columns[:pets].set_link('nested', :parameters => {:associations => "pets plants"})
    config.columns[:plants].set_link('nested', :parameters => {:associations => "pets plants"})

    #    uncomment these if you want to allow editing of pets and plants
    #    config.nested.add_link("Person's pets", [:pets])
    #    config.nested.add_link("Person's plants", [:plants])
  end
end

现在报告打开了,子表折叠了,所以我们必须使用protolicious中的event.simulate.js来扩展它们。

下载 protolicious 并将 event.simulate.js 复制到您的 public/javascripts 目录。

现在在你的布局中包含 event.simulate.js:

<%= javascript_include_tag :defaults %>
<%= javascript_include_tag "event.simulate.js" %>
<%= active_scaffold_includes %>

并将此脚本标记添加到视图的底部:

<script type="text/javascript">
  // iterates through each ActiveScaffold nested item link and clicks it
  $$('a.nested').each(function(link, index) {
    link.simulate('click');
  });
</script>

给定以下模型

应用程序/模型/person.rb

class Person < ActiveRecord::Base
  has_many :pets
  has_many :plants
end

应用程序/模型/pet.rb

class Pet < ActiveRecord::Base
  belongs_to :person
end

应用程序/模型/plant.rb

class Plant < ActiveRecord::Base
  belongs_to :person
end
于 2010-09-19T15:47:16.770 回答