[英]Custom Products Engine
我需要生成一个自定义引擎,例如按“品牌”和“类别”分类的产品:
产品介绍:
-----产品1 /品牌1 /中的相同产品
-----产品2
-第2类
-第3类
品牌:
-----产品1 /类别1 /中的相同产品
-----产品2
品牌2
品牌3
上面的最佳方案是什么?
我做了以下事情来满足自己的需求:
1-产生了一个新的引擎,称为category
$ rails generate refinery_engine category name:string image:image
$ bundle install
$ rails generate refinerycms_categories
2-产生了另一个引擎,称为品牌
$ rails generate refinery_engine brand name:string image:image
$ bundle install
$ rails generate refinerycms_brands
$ rake db:migrate
3-生成了名为产品的第三个引擎,并在类别和品牌引擎中生成了该引擎:
$ rails generate refinery_engine product category:engine brand:engine
name:string category_id:integer brand_id:integer number:string
quantity:integer brief:string description:text image:image
catalog:resource
$ bundle install
$ rails generate refinerycms_categories products
$ rake db:migrate
4-我已经在/ vendor / engines / categories / app / models中编辑了以下文件:
category.rb
has_many :products
product.rb
belongs_to :category
belongs_to :brand
5-我还编辑了/vendor/engines/brands/app/models/brand.rb:
has_many :products
6-我已经在_form.html.erb中编辑:category_id和:brand_id来制作HTML标记:
<div class='field'>
<%= f.label :category_id -%>
<%= collection_select(:product, :category_id, Category.all, :id, :name) %>
</div>
<div class='field'>
<%= f.label :brand_id -%>
<%= collection_select(:product, :brand_id, Brand.all, :id, :name)%>
</div>
7-在前端产品部分中,当我单击产品类别时,我会看到所有产品,并且品牌显示的是ID,而不是名称。 我需要显示名称而不是ID。
8-当我单击类别时,它仅列出类别而不显示其中的产品。...品牌也是如此。 因此,如何列出与每个类别和品牌相关的产品。
在此先感谢您的支持。感谢您的帮助。
创建一个单独的类别和品牌模型对我来说没有多大意义。 我希望将它们作为产品表中的字段,并编写一个帮助程序以获取所有唯一的类别和品牌。 例如:
def get_categories
Product.pluck(:category).uniq
end
要按类别过滤产品:(这将替换_form.html.erb中类别的选择标签)
options_for_select(get_categories)
要选择属于同一类别的产品,您可以执行以下操作:
@products = Product.where("category=?",params[:category])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.