admin管理员组

文章数量:1122846

I'm working on a project where I'm implementing Hotwire in this Rails 7 app that was migrated from one of the 6.1 versions. I have this table that I want to replace with the new version of that same table after I submit a new value from a dropdown. The thing is, that it works as expected with the first submit but then I can't even submit again, it's like it gets stuck or something.

This is the table:

# documents/_edit_tag_table.html.erb
<%= turbo_frame_tag "new_tag_#{tag_name}_#{issuer}" do %>
    <table class="table table-bordered" id="edit_table">
        <thead>
        <tr>
            <th scope="col">Tag</th>
            <th scope="col">Acciones</th>
        </tr>
        </thead>
        <%= render partial: "document_tags/document_tag", locals: {document: document, tag_name: tag_name, issuer: issuer} %>

    </table>
<% end %>

This is the partial contained in the previous code:

# document_tags/_document_tag.html.erb
<tbody id="tags-table-body">
    <% document.document_tags.each do |document_tag| %>
        <% if document_tag.tag&.tag_type&.name == tag_name %>
            <tr>
                <td>
                    <%= document_tag.tag.name %>
                </td>
                <td>
                    <%= button_to document_tag_path(document_tag, return_to: @redirect_url), method: :delete, data: { confirm: '¿Estás seguro que deseas eliminarlo?' }, class:"btn btn-outline-danger", remote: true do %>
                        <i class="fas fa-trash-alt"></i>
                    <% end %>
                </td>
            </tr>
        <% end %>
    <% end %>
    <tr> 
        <% document_tag = DocumentTag.new %>
        <%= form_with(model: document_tag, local: true, data: { turbo_stream: true }) do |form|%>
            <% if document_tag.errors.any? %>
                <div id="error_explanation">
                <h2><%= pluralize(document_tag.errors.count, "error") %> prohibited this document_tag from being saved:</h2>
                    <ul>
                    <% document_tag.errors.full_messages.each do |message| %>
                        <li><%= message %></li>
                    <% end %>
                    </ul>
                </div>
            <% end %>
            <td>
                <% tag_type = TagType.find_by(name: tag_name) %>
                <% if tag_type %>
                    <%= form.hidden_field :document_id, value: document.id %>
                    <%= form.hidden_field :tag_type, value: tag_name %>
                    <% if @redirect_url %>
                        <%= form.hidden_field :return_to, value: @redirect_url %>
                    <% end %>
                    <%= form.collection_select(:tag_id, Tag.where(tag_type_id: tag_type.id), :id, :name, {:prompt=>true}, {class: 'form-control js-example-basic-single', id: tag_type.id, onchange: "this.form.requestSubmit()"}) %>
                <% end %>
            <td>
            <%= form.submit "Agregar tag", class: 'btn btn-primary' %>
            </td>
        <% end %>
    </tr>
</tbody>

and this is the controller:

# document_tags_controller.rb
def create
      respond_to do |format|
        if @document_tag.save
          format.turbo_stream do
            render turbo_stream: turbo_stream.replace(
              "new_tag_#{@document_tag.tag.tag_type.name}_false", 
              partial: "documents/edit_tag_table", 
              locals: {
                document: @document_tag.document, 
                tag_name: @document_tag.tag.tag_type.name, 
                issuer: false 
              }
            )
          end
          format.html { redirect_to edit_document_path(@document_tag.document), notice: 'Se ha añadido el tag exitosamente.' }
          format.json { render :show, status: :created, location: @document_tag.document }
        else
          format.html { render :new }
          format.json { render json: @document_tag.errors, status: :unprocessable_entity }
        end
      end
    end

The table should be replaced everytime and item from the dropwdown is submited, but it only works with the first submit and then stops working, it doesn't even let me submit another item from the dropdown. What am I doing wrong?

本文标签: turboframeturbostream not working correctly on a recently migrated Rails 7 appStack Overflow