我正在尝试从 JavaScript 文件中访问 Rails Controller 中的“next”方法。从我读到的内容看来,我实际上需要将 js 转换为 js.erb 文件,并使用“asset_path”引用路径。但是,我不断在控制台中收到一条错误消息,显示“无效的正则表达式:缺少/”,该消息指向我所在的代码行:
$.get(<%= asset_path('next') %>, {id: projectId, next: 4}).done(function(response){
基本上,我只是想访问我的一个 Controller 中的“def next”方法。有更容易的方法吗??帮助!
最佳答案
正确的是,您的 erb 文件正在被编译为
$.get(/some/path/here, {id: projectId, next: 4}).done(function(response)
所以要修复它,请将其用引号引起来
$.get("<%= asset_path('next') %>", {id: projectId, next: 4}).done(function(response)
应该渲染到
$.get("/some/path/here", {id: projectId, next: 4}).done(function(response)
关于javascript - 从 javascript 访问 Rails 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37715542/