'Rails ancestry gem + Render and choose category/subcategory in new form
I have Rails 5.2.2 and I'm trying to implement the ancestry gem.
So what I want:
I want a user to create an offer for an automotive part, but in the new form I want to be able to choose the category/subcategories and then enter the other details that I have, and then submit the form to create the offer. Let's say that someone wants to add for selling Brake Pads. But first has to select the parent categories. E.g
Car -> Brakes -> Brake Pads
So after choosing Brake Pads, he can create the offer.
What I have:
#category.rb
class Category < ApplicationRecord
has_ancestry
has_many :parts
end
-
#part.rb
class Part < ApplicationRecord
belongs_to :category
end
For now, I have already created one in the console just to make it work: Eg
car = Category.create!(name: "Car")
brakes = Category.create!(name: "Brakes", parent: car)
brake_pads = Category.create!(name: "Brake Pads", parent: brakes)
I already also run the migration rails g migration add_category_to_parts category:references.
And my view:
#views/parts/new.html.haml
.container
%h2.center Create offer
= simple_form_for(@part, html: {class: "form-group" }) do |f|
.form-group.col-md-8.col-md-offset-2.well
= f.input :title
= f.input :make_name, label: "Make"
= f.input :code, label: 'Part number'
= f.association :condition, label_method: :name, prompt: "-"
= f.input :description
.form-actions
= f.button :submit, class: "btn btn-primary btn-dark-blue"
The question is: How I can render the categories/subcategories in my views-> parts -> new.html.haml form with 3 dropdowns(one for each subcategory because I will have many categories/subcategories) so the user can choose them and then create the offer?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
