I'm trying to figure out how to scope the options of one select field after an option of another select field is selected.
Once a board is selected in the 'boards' select field, only the lists that belong to that board should appear in the 'lists' select field.
A common example of this is seen on signup forms, where when a country is selected in the 'Country' select field, the 'State/Province' select field only shows the States/Provinces of that country.
Associations
class User < ActiveRecord::Base
has_many :boards
has_many :cards, through: :boards
end
class Board < ActiveRecord::Base
belongs_to :user
has_many :cards
end
class List < ActiveRecord::Base
belongs_to :board
has_many :cards
end
class Card < ActiveRecord::Base
belongs_to :board
end
Instance Variables used in the form
class CardsController < ApplicationController
def edit
@card = current_user.cards.find(params[:id])
@board = @card.board
@lists = @board.lists
end
end
Form
<%= form_for @card do |f| %>
...
#show the user's boards
<%= f.collection_select(:board_id, @boards, :id, :name) %>
#show the lists of a board
<%= f.collection_select(:list_id, @lists, :id, :name) %>
...
<% end %>