Reputation: 6371
I have two fields in my Rails application: language and word. The words table is quite large, and as such I'd like to have the selection filtered by the choice of language. Below is code from the edit view and you can see how I have hard-coded in the language in the second collection_select.
.field
= f.label :language_id
= f.collection_select :language_id, Language.find(:all,:conditions => ["supported = 't'"]),:id,:language_code, include_blank: false, :title => 'Language'
.field
= f.label :word
= f.collection_select :word_id, Word.find(:all,:conditions => ["language_id = 2"]), :id, :word, include_blank: false, :title => 'Word'
A similar question was posted on SO, but the solution provided was to use AJAX. I would prefer to reduce the database load by adding a criteria after the user can selected the language. Rails dependent collection_select fields in form
Is it possible for the Rails code to assess the chosen value of :language_id ?
Upvotes: 1
Views: 823
Reputation: 3700
Assuming the object containing the fields is Article :
= f.select :word_id, Word.where(:language_id => @article.language_id).map {|w| [w.name, w.id]}
Upvotes: 1