jQuery's getScript函数找不到我的.js.Rails中的动词模板

jQuery's getScript function can't find my .js.erb templates in Rails

本文关键字:Rails js 我的 getScript 找不到 函数 jQuery      更新时间:2023-09-26
$(function() {
  $("#ll_search").submit(function() {
    $.getScript(document.location.pathname + '/index.js.erb');
    return false;
  });
});

代码片段中的第3行导致以下错误:

ActionController::RoutingError (No route matches [GET] "/labs/index.js.erb").

我正在运行Rails 3.1,这意味着jQuery是默认的。我反复检查了一个文件"index.js. js"。"Erb"确实存在。jQuery似乎看不到它

请记住,document.location.pathname将为您提供根级路径,这将(默认情况下)在Rails中将您带到公共目录(除非存在相应的路由)。如果你需要在请求时呈现这个js文件(就性能而言不推荐),创建一个端点来呈现js并使用正确的MIME类型适当地返回它。

你可能不想要原始的ERB版本(没有在客户端会知道如何处理它)和脚本可能是在/assets在Rails 3.1所以尝试这些之一:

$.getScript('/labs/index.js');
$.getScript('/assets/labs/index.js');
$.getScript('/assets/index.js');

我猜了一下东西在哪里。