我有一张桌子,每一行都有一个链接
<%= link_to 'Delete', [lesson.group, lesson], remote: true,method: :delete%>
我希望链接销毁相应的db条目并从表中删除相应的行而不重新加载页面。 行动是
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"
)
答案 0 :(得分:2)
因为您没有处理销毁该对象后应该发生的事情。
respond_to do |format|
if @lesson
format.html { redirect_to edit_group_path(@group), notice:'Succesfully deleted lesson' }
format.js {}
# ...
format.js {}
在这里说&#34;用destroy.js.erb
或destroy.html.erb
或类似的&#34;来代替JS,根据你的设置,你没有设置错误(ActionView::MissingTemplate (Missing template application/destroy with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}
)。
您需要设置该模板,或呈现定义模板的其他操作(例如render action:
)。或者你可以做一个重定向(我不确定一个简单的redirect_to在这里工作)。但是请求需要&#34;结束&#34;不知何故,通过渲染或重定向。