I need to generate a custom engine like products which
are categorized in Brands and Categories:
Products:
- Category 1
—–Product 1 /the same product in brand 1/
—–Product 2
— Category 2
— Category 3
Brands:
- Brand 1
—–Product 1 /the same product in category 1/
—–Product 2
-
Brand 2
-
Brand 3
What are the best scenarios to the above.
I did the following to achieve my needs:
1- Generated a new engine called category
$ rails generate refinery_engine category name:string image:image
$ bundle install
$ rails generate refinerycms_categories
2- Generated another engine called brand
$ rails generate refinery_engine brand name:string image:image
$ bundle install
$ rails generate refinerycms_brands
$ rake db:migrate
3- Generated an third engine called product and genreated that inside
category and brand engines:
$ 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- I’ve edited the following files inside /vendor/engines/categories/
app/models:
category.rb
has_many :products
product.rb
belongs_to :category
belongs_to :brand
5- Also I’ve edited /vendor/engines/brands/app/models/brand.rb:
has_many :products
6- I’ve edited In _form.html.erb :category_id and :brand_id to make an
HTML tag:
<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-In the front-end product section I see all the products when I click
on a product Category and Brand are showing ids not the names. I need
to display names rather than ids.
8- When I click on categories it lists only categories without showing
the products inside it…. The same applies for brands. So how can I
list the products which are associated with categories and brands
inside each of them..
Thank you in advance for your support guys… I appreciate your help.
Creating a separate category and brands model doesn’t make much sense to me. I would rather have them as fields in the product table and write a helper to get all unique categories and brands.
eg:
To filter products by categories: (this would replace the select tag for categories in your _form.html.erb)
To select products belonging to the same category, you can do something like: