RoR:子记录列表

发布于 2024-09-18 01:01:50 字数 2512 浏览 6 评论 0原文

架构:

  • 人员(身份证、姓名、出生年份、性别)

  • 宠物(id,person_id,名称,leg_count)

  • 植物(id,person_id,种类,数量)

我想制作一份关于按人员分组的这些事物的只读报告。人员清单已完成(没有相关记录)。我想要每人都有“子表”。例如:

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< /code>、_list_column_headings.rhtml_list_actions.rhtml 一点点,以消除所有交互性(如排序等)。

Schema:

  • persons (id, name, birthyear, gender)

  • pets (id, person_id, name, leg_count)

  • plants (id, person_id, kind, qty)

I would like to make a read-only report about these things grouped by persons. The listing of personns is done (without the associated records). I would like to have "subtables" per persons. Something like:

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 |      |
| +----+------------+-----+      |
+----+------+-----------+--------+

Can you please help me with some tips where and how to hook to the framework? (JRuby (1.5.0), Ruby on Rails (2.3.4), ActiveRecord (2.3.4) )

What is done

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

It is done using:

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

And I also customized _list_header.rhtml, _list_column_headings.rhtml, and _list_actions.rhtml a little bit in order to kill all interactivity (like orderings and so on).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧幽蓝 2024-09-25 01:01:50

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

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

Models:

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

Controller:

reports_controller.rb:

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

View :

报告/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>

I don't understand the code you have written in your controller, nor why it inherits from your PersonsController. Maybe you can explain a bit more what you are trying to accomplish there?

That being said, I would solve your problem like this:

Models:

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

Controller:

reports_controller.rb:

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

View:

reports/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>
转身以后 2024-09-25 01:01:50

这是在 ActiveScaffold 中工作的解决方案:

app/controllers/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 并将 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>

给定以下模型

app/models/person.rb

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

app/models/pet.rb

class Pet < ActiveRecord::Base
  belongs_to :person
end

app/models/plant.rb

class Plant < ActiveRecord::Base
  belongs_to :person
end

Here's a solution that works inside ActiveScaffold:

app/controllers/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

Now the report opens up with the child tables collapsed, so we have to expand them using event.simulate.js from protolicious.

Download protolicious and copy event.simulate.js to your public/javascripts directory.

Now include event.simulate.js in your layout:

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

And add this script tag to the bottom of your view:

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

Given the following models

app/models/person.rb

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

app/models/pet.rb

class Pet < ActiveRecord::Base
  belongs_to :person
end

app/models/plant.rb

class Plant < ActiveRecord::Base
  belongs_to :person
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文