2

我的仓库:https ://github.com/czepesch/gloss (turboframes-dev 分支)

我有一个词汇表中的条目列表。删除操作有问题。条目已删除,但页面未重新加载\条目未自动删除。(相同的删除操作在同一页面上没有 turboframe)

glossaries/show.html.haml
= turbo_frame_tag 'tab_content' do

 - for entry in @glossary.entries
  = entry.term
  ...
  = link_to edit ...
  = link_to [@glossary, entry], data: { 'turbo_method': 'delete', turbo_confirm: "are you sure?" }
entries_controller.rb
  def destroy
    @entry.destroy

    respond_to do |format|
      format.turbo_stream { render turbo_stream: "turbo_stream.remove(@entry)" }
      format.html { redirect_to glossary_path(@glossary) }
      format.json { head :no_content }
    end
  end

服务器日志:

12:57:56 web.1  | Started DELETE "/glossaries/57/entries/8" for ::1 at 2022-01-29 12:57:56 +0100
12:57:56 web.1  | Processing by EntriesController#destroy as TURBO_STREAM
12:57:56 web.1  |   Parameters: {"glossary_id"=>"57", "id"=>"8"}
12:57:56 web.1  |   Glossary Load (0.1ms)  SELECT "glossaries".* FROM "glossaries" WHERE "glossaries"."id" = ? LIMIT ?  [["id", 57], ["LIMIT", 1]]
12:57:56 web.1  |   ↳ app/controllers/entries_controller.rb:57:in `get_glossary'
12:57:56 web.1  |   Entry Load (0.2ms)  SELECT "entries".* FROM "entries" WHERE "entries"."glossary_id" = ? AND "entries"."id" = ? LIMIT ?  [["glossary_id", 57], ["id", 8], ["LIMIT", 1]]
12:57:56 web.1  |   ↳ app/controllers/entries_controller.rb:61:in `set_entry'
12:57:56 web.1  |   TRANSACTION (0.1ms)  begin transaction
12:57:56 web.1  |   ↳ app/controllers/entries_controller.rb:46:in `destroy'
12:57:56 web.1  |   Entry Destroy (0.7ms)  DELETE FROM "entries" WHERE "entries"."id" = ?  [["id", 8]]
12:57:56 web.1  |   ↳ app/controllers/entries_controller.rb:46:in `destroy'
12:57:56 web.1  |   TRANSACTION (18.3ms)  commit transaction
12:57:56 web.1  |   ↳ app/controllers/entries_controller.rb:46:in `destroy'
12:57:56 web.1  | Completed 200 OK in 28ms (Views: 0.1ms | ActiveRecord: 19.4ms | Allocations: 2884)

在此处输入图像描述

4

1 回答 1

0

所以,我的问题主要在于我对框架和 id 的误解。在我稍微清理了我的代码之后,将一些部分移动到部分我已经解决了这个问题,只需给元素提供正确的标签。控制器保持不变:

entries_controller
respond_to do |format|
      format.turbo_stream { render turbo_stream: "turbo_stream.remove(@entry)" }

显示使用 turbo_frame_tag 移至部分的条目:

views/entries/_entry.html.haml
= turbo_frame_tag entry do
  - for entry in @glossary.entries
    = entry.term
    ...

在一个视图中,我创建了一个带有 id条目的 div

  %turbo-frame#entries
    = render @glossary.entries
于 2022-02-03T21:59:50.477 回答