0

我有一张桌子,每行都有一个链接

 <%= 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"
)
4

1 回答 1

2

因为您没有处理销毁该对象后应该发生的事情。

respond_to do |format|
        if @lesson
            format.html { redirect_to edit_group_path(@group), notice:'Succesfully deleted lesson' }
            format.js {}
          # ...

根据 您的错误( ) ,这里说“用或或类似的format.js {}方式响应 JS ”,您没有设置它。destroy.js.erbdestroy.html.erbActionView::MissingTemplate (Missing template application/destroy with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}

您要么需要设置该模板,要么render action:在定义模板的地方呈现另一个动作(例如)。或者你可以做一个重定向(我不确定一个简单的 redirect_to 是否在这里工作)。但是请求需要通过呈现或重定向以某种方式“结束”。

于 2013-10-24T14:46:53.947 回答