I have a requirement which is little difficult with how to start and i look for some help. I have three tables namely Apparels, categories and materials. Consider the apparels table contain
—– Mens wear
—– Children Wear
and i have a page to add the apparel and while adding apparels i need to have a drop down which should list the categories. On selecting the categories, the materials belonging to the selected category should appear in the multi-select box from which we can select the multiple materials which has to be saved in the table.
And the drop down to select the categories should be nested as we can also select more than one category and each time a category is added the multiple select drop down related to that category should be shown after the category drop down.
Consider the following image that clearly explains
How can i create a table for saving the values that i select from these tables?
Update:
class Apparel < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :apparels
has_and_belongs_to_many :materials
end
class Material < ActiveRecord::Base
has_and_belongs_to_many :categories
end
The above are the models and associations between those. I want to display a drop down menu and that should contain the categories and more over this drop down when selected the multi-select box should be displayed below each drop down to select the materials from it or tell me if i can do like keeping multi-select instead of drop down and on each select the another multi-select box should be populated with the values related to it. The following image will explain clearly
Best Answer
I would need a bit more info, especially some code examples to really help you. However here is some resources to get you started. When you have written something, then please come back and i/others can help you further :)
accepts_nested_attributes_for
. This is not necessarily important to you but will at least help you to understand how to create a multi record form.Update: How to dynamically populate a dropdown/select menu
Ok so what you want is to have a select menu (also called dropdown menu) of apparels, that updates each time you select a category (in a multiple select menu). To this can you use the method shown in Railscasts #88 - Dynamic select menus (revised), but let me explain more here:
First we want to create the view:
Then we add some javascript in the assets directory:
This is written in CoffeeScript with jQuery. If you don't use CoffeeScript, then write a comment and i will try to write it in the normal javascript syntax.