代码之家  ›  专栏  ›  技术社区  ›  Notinlist

子记录列表

  •  1
  • Notinlist  · 技术社区  · 14 年前

    架构:

    • 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)、RubyonRails(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 回复  |  直到 14 年前
        1
  •  5
  •   Mischa    14 年前

    我不明白您在控制器中编写的代码,也不明白它为什么继承自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
    

    class Plant < ActiveRecord::Base
      belongs_to :person
    end
    

    报告_控制器.rb:

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

    查看:

    报告/索引.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>
    
        2
  •  2
  •   marshally    14 年前

    下面是一个在ActiveScaffold中工作的解决方案:

    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 .

    现在包括事件.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>
    

    给定以下模型

    应用程序/模型/个人.rb

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

    应用程序/模型/宠物.rb

    class Pet < ActiveRecord::Base
      belongs_to :person
    end
    

    应用程序/模型/植物.rb

    class Plant < ActiveRecord::Base
      belongs_to :person
    end