0

I have a select_tag attribute within a ruby application. However, the default value obtains information from a database - Often that information is changed via an Ajax call and I was just wondering how one would be able to update a default value within a select_tag widget. Here is the following code:

= select_tag 'setup_form_popup', options_for_select([ ["No Setup Time","0"],["15 Minute", "15"], ["30 Minute", "30"], ["45 Minute", "45"], ["60 Minute", "60"], ["75 Minute", "75"], ["90 Minute","90"], ["105 Minute", "105"],["120 Minute","120"]], @event.setup_time), {:prompt => 'Setup Time'}

where @event.setup_time is the default when the page is run ... however when there is an Ajax request, it is not updated as the view is not reloaded. Any and all help is appreciated. Cheers~

Cummings.P
  • 53
  • 8
  • I think I will use jquery and update when it is changed ... any idea on how to set value? – Cummings.P Jun 15 '16 at 14:12
  • you have to do it through a ajax call(may be in the success callback) and change the selected value for this 'select'.http://stackoverflow.com/questions/314636/how-do-you-select-a-particular-option-in-a-select-element-in-jquery – sethi Jun 15 '16 at 14:12
  • ,update_setup_time: function(e) { var d = $('#setup_form_popup').val(); $.ajax({ beforeSend: function(xhr) { xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))}, url: '/events/'+ e.id + '/update_setup_time_p', type: 'GET', dataType: 'json', data: {needed: d}, success: function() { var intVal = parseInt(d); $('#setup_form_popup').val(intVal).change(); alert(intVal); TS.calendar.ajax_update(); }, }); } – Cummings.P Jun 15 '16 at 14:37
  • I put it in my success function, yet it is not updating as it should – Cummings.P Jun 15 '16 at 14:38
  • hmmm ... the view doesn't update either so i'm confused at why it is always defaulting to the initial default – Cummings.P Jun 15 '16 at 14:39
  • $('#setup_form_popup') is form or the select element? put a breakpoint if possible at var intVal = parseInt(d); and check if your selector is correct i.e. if $('#setup_form_popup') gives you the correct select element – sethi Jun 15 '16 at 14:52
  • I think because of the way my code is setup I might have to do this part in ruby ... is there a similar method that changes the val in ruby? is it just .val as well – Cummings.P Jun 15 '16 at 14:58
  • You can do it in ruby but remember ruby is running on the server not the client so you have to re-render the whole form and send it back as the response to the post ajax request and in the ajax success call $("#the-form").innerhtml(response) or a variant of that. – sethi Jun 16 '16 at 01:38

0 Answers0