簡體   English   中英

rails 4控制器重定向不加載javascript

[英]rails 4 controller redirect not loading javascript

我正在使用 Rails 4。我的root_url被路由到users#new 我想在用戶注銷時重定向到root_url ,這是在sessions控制器中完成的。 但是,重定向后,包含在users.js中的代碼的 javascript 文件沒有加載。 任何人都可以解釋為什么會發生這種情況? 如何解決?

class SessionsController < ApplicationController
    ...
    def destroy
        log_out
        redirect_to root_url
    end

如果這是因為users.js特定於控制器users ,那么加載 javascript 的重定向的最佳解決方案是什么?

順便說一句,在我的layouts/application.html.erb ,我添加了

<%= debug(params) if Rails.env.development? %>

在重定向頁面(即 root_url)上,它顯示:

--- !ruby/hash:ActionController::Parameters
controller: users
action: new

我想這意味着現在控制器是users 那么為什么沒有加載users.js

我需要一個教訓。

---- application.js .js ----

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-  directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

Turbolinks 可能會導致問題。 在您的 users.js 中,將代碼放入此塊中:

$(document).on('ready page:load', function() {
  // your code here
});

但是,如果您的 js 文件根本沒有加載,則上述解決方案可能不起作用。 如果您在瀏覽器中檢查頁面,您是否在資源下看到了您的 js?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM