【发布时间】:2017-04-18 14:06:06
【问题描述】:
我才刚刚开始学习 AJAX。我当前的 ajax 函数似乎没有任何效果。
这是我的咖啡脚本文件中的函数:
if $('.clickySeat').length > 0
$('.clickySeat').on "click", ->
goobergonk = $(this).find(".presentTag")
aulaId = $(this).attr('id')
url = '/aulas/update/?ref='+aulaId
if goobergonk.text() == "Absent"
attendance = true
$(this).removeClass("absent")
goobergonk.text("Present")
else
attendance = false
$(this).addClass("absent")
goobergonk.text("Absent")
$.ajax
type: "POST",
url: url,
dataType: "json",
data: {present: attendance},
success: alert(aulaId)
“Aulas”是连接“Student”表和“Seminar”表的模型(我使用“Seminar”这个词,因为我想避免“Class”这个词)。这是我将出勤数据存储为布尔值的地方。我对这个函数的目标是,当用户点击代表学生课桌(类“clickySeat”)的 div 时,表格会更新以跟踪学生是否在场。为了帮助 ajax 调用找到正确的行,我尝试为每个 div 分配其对应 aula 的 id。 “成功”警报消息表明该函数正在获取正确 aula 的 id。但是当我使用rails控制台检查“present”列中的值时,它并没有改变。
此代码包含“?ref=”作为 url 的 id 部分,因为它遵循我在其他网站上看到的大多数示例。我也试过没有那个位的版本。
我确定我在这里遗漏了一个关键的 ajax 语法。
提前感谢您的任何见解。
【问题讨论】:
标签: jquery ruby-on-rails ajax coffeescript rails-activerecord