mercredi 5 août 2015

jquery script not working well with turbolinks


I have an index in my app, whose view includes some filtering options. Once these are selected, results are being filtered. It's all nice except that when first getting into the index, all this scripting does not work and the page needs to be reloaded in order to function properly. I have tried using the jquery.turbolinks gem, didn't do anything. Any suggestions?

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') %>");



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire