为什么我的 rails AJAX 请求会抛出内部服务器错误
Why does my rails AJAX request throw internal server error?
我有一个表格,其中每行都有一个链接
<%= link_to 'Delete', [lesson.group, lesson], remote: true,method: :delete%>
我希望链接销毁相应的数据库条目并从表中删除相应的行,而无需重新加载页面。操作是
def destroy
@lesson = @group.lessons.find(params[:id])
@lesson.destroy
respond_to do |format|
if @lesson
format.html { redirect_to edit_group_path(@group), notice:'Succesfully deleted lesson' }
format.js {}
else
format.html { redirect_to edit_group_path(@group), notice:'Error!' }
end
end
end
条目被删除,但我只能在重新加载页面时看到更改,并且控制台抛出
DELETE http://localhost:3000/groups/1/lessons/14 500 (Internal Server Error)
从日志中提取:
Started DELETE "/groups/1/lessons/18" for 127.0.0.1 at 2013-10-24 18:39:22 +0400
Processing by LessonsController#destroy as JS
Parameters: {"group_id"=>"1", "id"=>"18"}
[1m[36mGroup Load (1.0ms)[0m [1mSELECT "groups".* FROM "groups" WHERE "groups"."id" = ? LIMIT 1[0m [["id", "1"]]
[1m[35mLesson Load (1.0ms)[0m SELECT "lessons".* FROM "lessons" WHERE "lessons"."group_id" = 1 AND "lessons"."id" = ? LIMIT 1 [["id", "18"]]
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mSQL (4.0ms)[0m DELETE FROM "lessons" WHERE "lessons"."id" = ? [["id", 18]]
[1m[36m (10.0ms)[0m [1mcommit transaction[0m
Completed 500 Internal Server Error in 35ms
ActionView::MissingTemplate (Missing template lessons/destroy, application/destroy with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "C:/Sites/timetable/app/views"
)
因为你没有处理销毁该对象后应该发生的情况。
respond_to do |format|
if @lesson
format.html { redirect_to edit_group_path(@group), notice:'Succesfully deleted lesson' }
format.js {}
# ...
这里的format.js {}
说"与 destroy.js.erb
或 destroy.html.erb
或类似的东西产生共鸣",根据您的错误(ActionView::MissingTemplate (Missing template application/destroy with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}
),您尚未设置。
您要么需要设置该模板,要么渲染另一个动作(例如 render action:
),其中定义了模板。或者你可以做一个重定向(我不确定一个简单的redirect_to在这里是否有效)。但是请求需要以某种方式"结束",无论是通过渲染还是重定向。
相关文章:
- SignalR:调用集线器方法时出现内部服务器错误
- 如何解决“;错误所请求的URL返回500-内部服务器错误”;
- 使用javascript API响应内部服务器错误将曲目上传到SoundCloud
- AJAX错误状态代码500内部服务器错误
- Jcrop&画布:裁剪区域的大小很奇怪;500内部服务器错误
- 如果JS会话变量为null,如何避免500内部服务器错误
- POST encodeURI查询字符串内部服务器错误 500
- getJSON:网络错误:500 内部服务器错误
- Node.js:Mongoose内部服务器错误
- 缺少用于创建的模板.内部服务器错误-聚合物表单保存到数据库
- Jquery AJAX Post:500(内部服务器错误)
- CSS和Javascript可以在内部服务器上工作,但不能在外部工作
- 我的联系表格在测试/PHPMailer后给了我一个内部服务器错误(500)
- 500(内部服务器错误)的外部php文件
- 内部服务器错误在 post 请求中使用 Angular js 中的自定义服务
- 内部服务器错误:将 JavaScript 对象传递给 C#
- 为什么我的 rails AJAX 请求会抛出内部服务器错误
- 500 asp.net 内部服务器错误
- 500 内部服务器错误:无法更新猫鼬中的用户
- 在尝试将插入值从javascript调用到Visual Studio中的sql服务器时,收到内部服务器错误500