0

I am building a rails form and have an interesting problem I am trying to solve. I can't seem to find anything online to point me in the right direction. Thank you.

Is it possible to use a dropdown menu to select the :object_name for a text field? In my head, I am picturing a collection_select form helper nested within a text_field form helper, though not sure this is possible.

In the form, I'd like the user to select the proper :object_name from an array

[:object_1, :object_2, :object_3, :object_4]

then give that entry a value with the text field

text_field(object_name, method, options = {})

The objects are all db columns in the same model.

ncarroll
  • 108
  • 1
  • 1
  • 10

1 Answers1

0

Yes you can do that using jquery.

On change of the object names dropdown value, change the name attribute of the text field.

$('#selectObjectName').change(function(){
  var field = document.getElementById("id-of-the-text-field-to-be-changed");
  field.setAttribute("name", "value-came-from-the-selected-dropdown"); 
})
Emu
  • 5,763
  • 3
  • 31
  • 51