Using the simple form gem for Ruby on Rails, I have a form that looks like this:
<%= simple_form_for(article, html: {class: 'form-vertical'}) do |f| %>
...
<div class="field">
<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category"} %>
<%= f.collection_select :subcategory_id, Subcategory.all, :id, :name, {prompt: "Choose a subcategory"} %>
<%= f.input :title %>
<%= f.input :content, input_html: { rows: 20 }%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Is there a way to base the options in the subcategory input on the selection in the category input? What I mean is, if I had a category "food", then I would be able to pick a subcategory "apple", but not "table". Would this be possible?
Category model:
class Category < ApplicationRecord
has_many :articles
has_many :subcategories
end
Subcategory model:
class Subcategory < ApplicationRecord
belongs_to :category
end