Last week I was trying to setup dynamic select boxes in rails. At the begining I tried to use collection_select
form helper to setup both the select boxes. I was planning to do the filtering using JS. But I was unsuccessful in that. Then while I went through the rails API, I read about grouped_collection_select
which can filter the data automatically if the relation is defined. So i wanted to use that.
Lets setup a sample application to do this.
$ rails new dynamic_boxes
This will create a new application. Lets setup dynamic select boxes for categories and subcategories. I have to setup an Article model which belongs to a category and subcategory. So we’ll have 3 models - Article, Category and Subcategory.
dynamic_boxes]$ rails g model Category name
dynamic_boxes]$ rails g model Subcategory name category_id:integer
dynamic_boxes]$ rails g scaffold Article name content:text category_id:integer subcategory_id:integer
app/models/category.rb
app/models/subcategory.rb
app/models/article.rb
These will create the models and the controller and views for article. Now we go to the articles view _form.html.erb
app/view/articles/_form.html.erb
Now seed some data. Then in the new articles page we can find that the dropdown of subcategories is filtered by its respective categories. Now we can write some coffeescript to filter the subcategory options according to the selected category.
app/assets/javascripts/articles.js.coffee
And thats it. Now if you try to select a category, the subcategories will get filtered automatically.
You can check the sample here.