A
A
Andrey2017-07-27 15:15:27
JavaScript
Andrey, 2017-07-27 15:15:27

How to render select on ajax request?

The bottom line is this: I select the first one selectand, depending on the value value(from the first select), I build the second one select. those. I send a get-request with a parameter codeto the controller and I want to receive the array I need in response. I need to get data from a controller.

The data is sent to the controller. There, on the basis of this data, I have an array The @point

question is: How to render now this in a view. In partial, @pointthere is a variable with the desired values ​​​​(when I chose the city). More like using jsit to render.

My JS:

$(document).ready(function () {
 $('#country_id').change(function () {
      var country_id = $(this).val();
      $('#region_id').attr('disabled', true);
      $('#region_id').html('<option>загрузка...</option>');
      $.get(
          '/baskets',
          { code: country_id },
          function (result) {
             if (result.type == 'error') {
                   alert('error'); js
                  return(false);
              }
              else {
                  // вот эта часть не рендерится   
                  $('<%= j(render :partial => "points", :locals => {:point => @point}) -%>').appendTo('#country_id');
                  $('#region_id').attr('disabled', false);
              }Паршл point
          },
          "json"
      );
  });
});


index.html.erb

js
<form action="/baskets" >
<p>Город</p>
<select name="country_id" id="country_id">Паршл point
    <option value="0">- выберите город -</option>
    <% @city.sort.each do |city| %>
      <option value="<%=city[1]%>"><%= city[0] %> </option>
    <% end %>
</select>
<br><br>
<p>Пункт выдачи:</p>
 <%= render :partial => 'points', :locals => {:point => @point} %>
</form>


Parsley point

<select name="region_id" id="region_id" disabled="disabled">
  <option value="0">- выберите пункт -</option>
  <% @point.sort.each do |point| %>
    <option value="<%=point[1]%>"><%= point[0] %> </option>
  <% end %>
</select>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question