Jquery脚本不能很好地使用涡轮链接

jquery script not working well with turbolinks

本文关键字:链接 脚本 不能 很好 Jquery      更新时间:2023-09-26

我有一个索引在我的应用程序,其视图包括一些过滤选项。一旦这些被选中,结果将被过滤。这一切都很好,只是在第一次进入索引时,所有这些脚本都不起作用,并且需要重新加载页面才能正常工作。我试过使用jquery.turbolinks宝石,没有做任何事情。有什么建议吗?

index.html.erb:

<% content_for :on_document_ready_js_script do %> #this being yielded on layout
    $("#er_filters").change(function() {
        var url = "/examinee_registrations.js";  // grab the URL from the form action value.
    $.get(url, $("form").serialize()); 
   });
<% end %>

<div id="title">
    <h1> 
        <%=t("examinee_registration.title")%>
        <% if @location_id %>
            <span id="<%=@location_id%>"> <%=t("for") + " #{Location.find(@location_id).name}" %> </span>
        <% end %>
    </h1>
</div>
<br>
    <%= form_tag examinee_registrations_url(format: "xls"), method: :get do %>
        <div id="er_filters">
            <br>
            <%= hidden_field_tag 'location_id', @location_id %>
            <%= label_tag 'from_date', t("from_date")+':' %>
            <%= date_field_tag "from_date" %>
            <br>
            <%= label_tag 'to_date', t("to_date")+':' %>
            <%= date_field_tag "to_date" %>
            <br>
            <%= label_tag 'hide_open', t("examinee_registration.hide_open")+':' %>
            <%= check_box_tag "hide_open", true, false  %>
            <br>
            <%= label_tag 'hide_closed', t("examinee_registration.hide_closed")+':' %>
            <%= check_box_tag "hide_closed", true, false  %>
        </div>
        <%= submit_tag t('export_to_excel') %>
    <% end %>
<br>
<%= link_to create_title('examinee_registration.single')+"ה", new_examinee_path %>
<hr>

<div id='er_div' >
    <%= render 'examinee_registrations' %>
</div>

application.js:

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .

index.js.erb:

$("#er_div").html("<%= escape_javascript(render 'examinee_registrations') %>");

为什么要使用涡轮链接?除非你有一个非常好的,经过深思熟虑的理由,只有涡轮链接才是答案,否则我建议你放弃它。