gpt4 book ai didi

javascript - 为什么我的 Rails AJAX 请求会引发内部服务器错误?

转载 作者:行者123 更新时间:2023-12-02 18:12:51 25 4
gpt4 key购买 nike

我有一个表格,其中每一行都有一个链接

 <%= 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.erbdestroy.html.erb 或类似内容响应 JS ”,根据您的错误(ActionView::MissingTemplate(Missing template application/destroy with {:locale=>[:en], :formats=>[:js, :html ], :handlers=>[:erb, :builder, :coffee]})。

您需要设置该模板,或者在定义模板的情况下渲染另一个操作(例如 render action:)。或者你可以做一个重定向(我不确定一个简单的redirect_to在这里是否有效)。但请求需要以某种方式“结束”,无论是通过渲染还是重定向。

关于javascript - 为什么我的 Rails AJAX 请求会引发内部服务器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19568667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com