RoR:子记录列表

时间:2010-09-06 11:19:15

标签: ruby-on-rails ruby activerecord jruby activescaffold

架构:

  • 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一点点,以便消除所有交互性(如排序等)。

2 个答案:

答案 0 :(得分:5)

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

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

<强>型号:

person.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

pet.rb:

class Pet < ActiveRecord::Base
  belongs_to :person
end

plant.rb:

class Plant < ActiveRecord::Base
  belongs_to :person
end

<强>控制器:

reports_controller.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>

答案 1 :(得分: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

现在报告打开,子表已折叠,因此我们必须使用event.simulate.js中的protolicious展开它们。

下载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