Ruby on rails 在Rails中显示数据库值?

Ruby on rails 在Rails中显示数据库值?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我的桌子是: | 1 | Electronics | Cameras | 2 | Electronics | Computers | 3 | Electronics | Mobile Phone | 4 | Electronics | Sound & Vision | 5 | Clothes | womens clothing | 7 | Clothes | wome

我的桌子是:

|  1 | Electronics     | Cameras         
|  2 | Electronics     | Computers       
|  3 | Electronics     | Mobile Phone    
|  4 | Electronics     | Sound & Vision  
|  5 | Clothes         | womens clothing 
|  7 | Clothes         | women shoes     
|  8 | Clothes         | women hand bags 
|  9 | Clothes         | Mens clothing   
| 10 | Clothes         | Kids clothing  
我试图在我的索引页上显示如下链接:

**Electronics**
Computers
Mobile Phone 
Sound & Vision
**Clothes**
womens clothing
women shoes 
Mens clothing 
class Category < ActiveRecord::Base
  has_many :products
   attr_accessible :division,:subdivision
  end 


class Product < ActiveRecord::Base
        has_many :photos,:dependent => :destroy
        has_many :line_items, :through => :product_id
        belongs_to :merchant
        belongs_to  :category
        attr_accessor :quantity
        accepts_nested_attributes_for :photos, :line_items
我该怎么做

我的模型是这样的:

**Electronics**
Computers
Mobile Phone 
Sound & Vision
**Clothes**
womens clothing
women shoes 
Mens clothing 
class Category < ActiveRecord::Base
  has_many :products
   attr_accessible :division,:subdivision
  end 


class Product < ActiveRecord::Base
        has_many :photos,:dependent => :destroy
        has_many :line_items, :through => :product_id
        belongs_to :merchant
        belongs_to  :category
        attr_accessor :quantity
        accepts_nested_attributes_for :photos, :line_items
类别:销毁
有多个:行\u项,:至=>:产品\u id
属于:商人
属于:类别
属性存取器:数量
接受\u嵌套的\u属性\u用于:照片、:行\u项目
类别表是父项,产品具有
Category\u id
。仅当存在任何产品且我的产品型号如下所示时,我希望以上述方式显示类别:

**Electronics**
Computers
Mobile Phone 
Sound & Vision
**Clothes**
womens clothing
women shoes 
Mens clothing 
class Category < ActiveRecord::Base
  has_many :products
   attr_accessible :division,:subdivision
  end 


class Product < ActiveRecord::Base
        has_many :photos,:dependent => :destroy
        has_many :line_items, :through => :product_id
        belongs_to :merchant
        belongs_to  :category
        attr_accessor :quantity
        accepts_nested_attributes_for :photos, :line_items
当我点击链接显示链接时,我计划显示所有相关产品


有人能告诉我如何显示有产品的类别吗?

您可以使用Enumerable#group by轻松完成此操作





传递给group_的块将被计算,每个计算为相同值的记录将被分组在一个哈希键下,您可以使用该哈希键进行循环。每个

这都是一个视图问题。你能告诉我们你的视图/控制器文件是什么样子的吗?我还没有创建视图。但是在我的类别中,控制器看起来像这样,我在show.html.erb中看到了数据。但是我不知道如何在我的主页index.html.erb pageclass CategoryController