【发布时间】:2015-08-02 14:40:10
【问题描述】:
我正在使用 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 ----
// 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 .
【问题讨论】:
-
你的
application.js里面有什么? -
谢谢 naylaw。我已经添加了
-
默认情况下,每个页面加载时都会加载每个js文件。没有特定于页面的范围。 Rails 的常见嫌疑人是 Turbolinks。从 application.js 中删除
//= require turbolinks行,看看它是否有效。 -
嗯,您能否更具体地说明
users.js未加载?users.js里面的代码不起作用或者本地开发环境下浏览器开发工具看不到文件?如果是代码不起作用,您也可以分享users.js吗? -
@zkytony:构建 js 文件以按页面/控制器加载并不是那么简单。 brandonhilkert.com/blog/… 可能会有所帮助。
标签: javascript jquery ruby-on-rails redirect