2

My repo: https://github.com/czepesch/gloss (turboframes-dev branch)

I have a list of entries in a glossary. Problem with delete action. Entry is deleted but page not reloading\entry not removing automatically. (same delete action working without turboframe on the same page)

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

server log:

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)

enter image description here

Alexander V
  • 45
  • 1
  • 5

1 Answers1

0

So, my problem was mostly with my misunderstanding frames and id's. After I've cleaned my code a bit, moved some parts to the partials I've resolved the issue just by given correct tags to elements. Controller remained the same:

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

displaying the entries moved to partial with turbo_frame_tag:

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

and in a view I've created a div with id entries

  %turbo-frame#entries
    = render @glossary.entries
Alexander V
  • 45
  • 1
  • 5