-
Notifications
You must be signed in to change notification settings - Fork 4
Examples
russjohnson edited this page Sep 13, 2010
·
1 revision
tables
car_companies: id, name car_models: id, name, car_company_idview
<%= collection_select(:car_company, :id, CarCompany.find(:all), :id, :name) %> <%= related_collection_select( :car_model, :id, [:car_company, :id], CarModel.find(:all), :id, :name, :car_company_id) %>The code above will create two drop-down select tags. The 1st allows the
selection of a car company. Based on this decision the 2nd select tag shows
company specific car models.
h2.Example: Three related select forms
The script allows to generate an almost unlimited number of related selects:
tables
categories: id, title, parent_id some_objects: id, name, category_idcontroller
@first_col = Category.find(:all, :conditions => ‘parent_id IS NULL’) @second_col = Category.find(:all, :conditions => ‘parent_id > 0’) @third_col = SomeObject.find(:all) @some_object = SomeObject.find(123)view
<%= collection_select(:category, :id, @first_col, :id, :title) %> <%= related_collection_select(:sub_category, :id, [:category, :id], @second_col, :id, :title, :parent_id) %> <%= related_collection_select(:some_object, :id, [:sub_category, :id], @third_col, :id, :name, :category_id, {}, {:size => 6}) %>This example creates three related select tags. The 1st shows ‘categories’,
the 2nd ‘sub-categories’ and the 3rd ‘some_objects’. Because an ‘@some_object’
instence variable is defined, item with id=‘123’ will be pre-selected
automatically in the 3rd select tag. The 1st & 2nd select forms will also be
recursively adapted automatically.