我想使用ajax从我的控制器获取信息。基本上我想知道数据库中是否已经存在数据,因此我的控制器将返回true或false。 目前我只是想设置基本AJAX调用ajax从轨道控制器获取数据
在我的js文件中我有以下ajax调用,你可以看到此刻我没有真正做任何逻辑,因为我的数据只是一个占位符。后来,我将添加我想查询
$.ajax({
type: "GET",
url: "/locations/exists",
dataType: "JSON",
data: { 'locition_exist': loc_exit },
success: function(data) {
console.log(data);
}
});
在我的控制器的信息,我有
def exists
location_exists = true
respond_to do |format|
format.html
format.json {render json: location_exists }
end
end
后面的值将进入一个方法,将查询数据库,并返回真/假模型。
在我的路线我有
resources :locations
get 'locations/exists', to: 'locations#exists'
以下错误
The action 'show' could not be found for LocationsController
我新的Rails和AJAX这个代码的结果,我根据我读到这里不同的例子我的代码,所以我可能只是在做一个愚蠢的noob错误。 非常感谢您的帮助
我已更新我的帖子中的代码。这仍然导致相同的错误。 – Quantico
谢谢您的详细解答。我会阅读更多关于收集更好地了解你的建议:) – Quantico
@Quantico,我的荣幸:)你可以在这里阅读文档:http: –