Google API自动完成功能无法在Rails中使用,但在重新加载后可以使用

时间:2019-07-16 11:26:52

标签: ruby google-maps-api-3 google-api ruby-on-rails-5

我正在尝试添加google autocomple api,但是它仅在页面重新加载后才有效,有时即使在``page reload,以下代码之后也无法使用:

<%= javascript_include_tag 'https://maps.googleapis.com/maps/api/js?key=' + Rails.application.secrets.google_api_key + '&libraries=places' %>
<div class="mui-textfield" data-turbolinks="false">
    <input type="hidden" name="id" value='' id="user_id">
    <%= text_field_tag :location, nil, id: 'txtPlaces', class: "form-text", placeholder: '', required: true %>
        <%= hidden_field_tag :full_address, nil, id: 'full_address', class: "form-text", placeholder: '' %>
</div>
<script>
  var ready = function () {

    var options = {
          type:['(regions)'],
          componentRestrictions: {
            country: ['au']
          }
        };

        google.maps.event.addDomListener(window, 'load', function () {
            var input = document.getElementById('txtPlaces');
            var places = new google.maps.places.Autocomplete(input, options);
            google.maps.event.addListener(places, 'place_changed', function () {
               $("#full_address").val($("#txtPlaces").val());
            });
          });
      // This is some custom requirement
        $('.save-address').on('click', function(e){

          if($("#full_address").val() !== $("#txtPlaces").val()){
            e.preventDefault();
            e.stopPropagation();
            $("#txtPlaces").val('');
            $(this).submit();
          }
        });
    };
    <%#*$(document).ready(ready);%>
    <%#*$(document).on('page:load', ready);%>
    $(document).on("turbolinks:load", ready);
</script>

我也尝试过上述 ready和page:load 功能,但没有运气 除此之外,下面是到达上一页的代码

<li><%= link_to('Active', admin_staffs_path(status: 'accepted'), data: { turbolinks: false })

0 个答案:

没有答案