在 ruby on Rails 的 html.erb
文件中的 Onclick 方法时,我遇到以下问题。
Uncaught ReferenceError: modifyGroup is not defined at HTMLButtonElement.onclick (new:48141)
我在js文件中编写了搜索和其他方法,它们都工作正常。但这种方法只是我没有收到此错误。
function modifyGroup() {
var groupname = document.getElementById("groupname").value;
var parametersname=document.getElementById("object_test").value;
$.ajax({
method: "GET",
dataType: "text",
url: "/group/update_group",
data: {
groupname: groupname,
parametersname: parametersname,
},
success: function(result) {
if(result === 'false') {
alert('Group Could not be Updated !!!');
return false;
} else{
alert('Group Updated successfully !!!');
window.location='/group/new';
}
}
});
}
最佳答案
请使用
将 js 文件包含在您的 View 中<%= javascript_include_tag "filename_js", "data-turbolinks-track" => true %>
请在assets.rb中添加你的js文件
Rails.application.config.assets.precompile += %w( filename_js.js )
然后运行
rake assets:precompile
并启动你的rails服务器
关于javascript - JavaScript 和 Rails 中未定义 Uncaught ReferenceError 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53912499/