-3

I am trying to get either a dropdown or selection box(prefer the later, because of the possibility to choose multiple values at once) in a webform. In this case i already got the dropdown working, based on measures.measurement_type. The second needs to be measures.measurement, filtered by the type selected in the first dropdown. I can not seem to get this working. I tried googling, but without succes. Can anyone help me get on the right track? I found solutions using Arrays, but no working solution using 1 database table.

using ruby 4.2 Thanks

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Marco
  • 75
  • 1
  • 13
  • any code? what have you tried so far? I'd say you will need JS for that – Fallenhero Dec 15 '16 at 15:50
  • First of all, there no ruby 4.2 version. I'm guessing it's a typo. Second, I did a simple google search with your question's heading and i found two stackoverflow's answers with exact result what you are looking for. Next time, Please try to google it out and than post the question – Pramod Dec 15 '16 at 16:37
  • Possible duplicate of [Populate one dropdown list based on the selection of other dropdown list](http://stackoverflow.com/questions/19039740/populate-one-dropdown-list-based-on-the-selection-of-other-dropdown-list) – Pramod Dec 15 '16 at 16:40
  • i also googled before posting this question, but those 2 options you mentioned didnt seem to work. Or atleast, i could not get them to work. – Marco Dec 19 '16 at 08:13
  • Or atleast, i could not get them to work. Most examples use a second table or array. I need to use 1 tabe and fill the second item based on basicaly a select * from measurements where measurement_type = dropbox1.value – Marco Dec 19 '16 at 08:19

2 Answers2

0

You gotta use some ajax for doing that, can't see other way. When your measures.measurement_type changes, you send a request passing that measurement_type as param for your action. In that action, you retrieve the collection of measurements based on the measurement_type passed in the param, and then return that data to be handled on your success ajax callback. On that method, with some jquery you should populate the second input with the options returned.

This is some simpler explanation... you should take some look at a more complete article for understanding step-by-step. Would suggest this one, for example: https://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax. Good luck!

Ronan Lopes
  • 3,320
  • 4
  • 25
  • 51
  • Thanks, will look into that. Have never used Ajax before so i didn't think of that. – Marco Dec 19 '16 at 08:20
  • And nevermind for now. Just noticed that the data in the second table is not saving at all. That needs to be fixed first. – Marco Dec 20 '16 at 09:41
0

There are two options for this.

  1. Using AJAX calls. Like @Ronan said in his answer, you need to make an AJAX call on the selection of first drop down (on change method). In the rails action method, you can render a JS partial where you can set the filtered items for second drop down.

  2. Another one is totally client side. Like render all the possible items of both drop downs to client side. Meaning both type and measurements as javascript array. Then when changing the type drop down, filter the measurements array using jQuery and populate in the second drop down.

Jayaprakash
  • 1,407
  • 1
  • 9
  • 19