Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rails重定向后Javascript不执行_Javascript_Jquery_Ruby On Rails_Redirect - Fatal编程技术网

Rails重定向后Javascript不执行

Rails重定向后Javascript不执行,javascript,jquery,ruby-on-rails,redirect,Javascript,Jquery,Ruby On Rails,Redirect,我的应用程序包含一个非常简单的登录和两个单独的AJAX调用。我有一个加载微调器,每当页面准备就绪时,它就会自动隐藏在index.js中 $(function() { $("loading").hide(); $("#scan_button").on("ajax:success", function(e, data, status, xhr) { $("#standard_results").hide(); $("#results").show();

我的应用程序包含一个非常简单的登录和两个单独的AJAX调用。我有一个加载微调器,每当页面准备就绪时,它就会自动隐藏在index.js中

$(function() {

   $("loading").hide();

   $("#scan_button").on("ajax:success", function(e, data, status, xhr) {
       $("#standard_results").hide();
       $("#results").show();
       $("#results").html(data);
   });

   $(document).ajaxStop(function() {
       $("#loading").hide();
   });

   $(document).ajaxStart(function() {
       $('#loading').show();
   });

});
当用户登录时,会发生以下情况

class SessionController < ApplicationController

    def create
        user = User.find_by(username: params[:session][:username])

        if(user.password == params[:session][:password])
            log_in user
            redirect_to root_path
        end
    end

    def destroy
        logout
        redirect_to root_path
    end

end
class SessionController
路由路径将我带回index.js文件所在的“home#index”。当用户登录重定向时,加载微调器被隐藏,所有JavaScript按预期执行

然而,当用户点击log out时,会发生重定向,但会显示加载微调器,并且不会执行JavaScript。这是我的index.html.erb文件,其中包含JavaScript

<% content_for :title, "Network Monitor" %>


<div class="container-fluid">

    <nav class="navbar narvbar-dark bg-inverse">
        <span class="navbar-brand">Network Monitor</span>

        <%= link_to "Scan", scan_path, remote: true, class: "btn btn-secondary", id: "scan_button" %> 


        <% if logged_in? %>

            <span class="pull-xs-right"><%= link_to "Logout", logout_path, class: "btn btn-secondary" %></span>

        <% else %>

            <%= form_for(:session, url: login_path, :html => {:class => 'form-inline pull-xs-right'}) do |f| %>

                <%= f.text_field :username, class: 'form-control', placeholder: "Username" %>
                <%= f.password_field :password, class: 'form-control', placeholder: "Password" %>
                <%= f.submit "Log in", class: 'btn btn-primary', id: 'login_button' %>

            <% end %>
        <% end %>
    </nav>

    <div id="loading">
        <%= image_tag "loading.gif", class: "loading_gif" %>  
    </div>

    <div class="row">
        <div class="col-md-12">
            <div id="scan_bar">
                <h3>Welcome to your network monitor!</h3> <br />

                <p>To get started hit the scan button in the top right corner, this will detect the computers that are alive on your network and show them below.</p>
            </div>
        </div>
    </div>

    <div class="row">
        <div id="results">
    </div>

  <div id="standard_results">

  </div>

网络监视器
{:class=>'forminlinepullxsright'})do | f |%>
欢迎使用您的网络监视器
要开始,请点击右上角的扫描按钮,这将检测到网络上的计算机,并在下面显示它们


已解决。它就像使用

$(document).on('turbolinks:load', function() {});
与我之前发布的内容不同,这似乎是Rails 5和turbolinks希望您使用的方式,而且很有效。

根据 文档中,我使用事件
页面:change
来解决问题:

$(document).on("page:change", function(){...});

这对我很有效。

以防其他人遇到这种情况。如果您使用的是
href
链接_
,请尝试执行以下操作:

      <li><%= link_to "Home", jobs_path, method: :get  %></li>
  • …其中指定请求中的get方法