Search code examples
jqueryruby-on-railsrubyajaxsimple-form

Ruby on Rails - Update collection_select while filling out the form


My application works as I have 4 records with same fields (:name, :family_with) in my form already that users needs to fill out with the :name.

(PS: I am using nested forms and cocoon gem).

Here is my form:

= f.simple_fields_for :families do |family|
   = f.text_field :name
   = f.collection_select :family_with, Family.where(family_id: @family), :id, :name, {include_blank: "Choose a name"}

What I am looking for is, how can I update collection_select :family_with every time a f.text_field :name is fill, so my collection_select :family_with can populate the name's that has been inserted in each = f.text_field :name.

I have read that I could do it with ajax or jQuery but would appreciate any code solution.


Solution

  • Is it this?

    = f.collection_select :family_with, Family.all.where(family_id: @family), :id, :name, {include_blank: "Choose a name"},selected: f.object.family_with