0

当我尝试更新服务并提交所有更新时,除了带有旧值的 SQL 查询的名称。

编辑表格:https ://i.stack.imgur.com/YSZuD.png

如果我提交这个,我会得到这个:

      ↳ app/controllers/services_controller.rb:133:in `block in update'
  Service Exists? (5.4ms)  SELECT 1 AS one FROM `services` WHERE `services`.`name` = 'Tratamento 2' AND `services`.`id` != 2 LIMIT 1
  ↳ app/controllers/services_controller.rb:133:in `block in update'
  Service Exists? (3.9ms)  SELECT 1 AS one FROM `services` WHERE `services`.`ref` = 'test' AND `services`.`id` != 2 LIMIT 1
  ↳ app/controllers/services_controller.rb:133:in `block in update'
  Service Update (3.9ms)  UPDATE `services` SET `services`.`ref` = 'test', `services`.`description` = 'test', `services`.`updated_at` = '2021-12-29 11:05:20.531023' WHERE `services`.`id` = 2

ref获取新值但name搜索“Tratamento 2”(旧值)。

/services/_edit.html.erb

<%= turbo_frame_tag :edit_service_details do %>
  <div data-controller="unblind-modal">
    <div data-unblind-modal-target="background" data-popup-confirm-delete-target="background" class="modal-background" style="z-index: 9998;"></div>
    <div data-unblind-modal-target="content" data-target-modal="edit_details" class="fixed inset-0 overflow-y-auto flex items-center justify-center m-6" style="z-index: 9999;">
      <%= form_with model: @service, url: polymorphic_path([@service], { business_id: @service.business.id }), class: " relative w-full flex flex-col items-center justify-center overflow-auto bg-white rounded-lg max-w-screen-sm" do |form| %>
        <div class="w-full sticky top-0 flex items-center">
          <div class="mx-6 py-4 w-full flex justify-between items-center">
            <h1 class="txt_heading text-neutral-100 text-neutral-80">Edit Details</h1>
            <div data-action="click->unblind-modal#close" data-target-modal="edit_details" class="btn-secondary btn-md flex items-center w-9 px-0 justify-center">
              <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M6 18L18 6M6 6l12 12"/>
              </svg>
            </div>
          </div>
        </div>
        <div class="w-full h-auto flex flex-wrap md:flex-nowrap overflow-auto">
          <div class="w-full overflow-auto">
            <div class="px-6 pb-4 overflow-auto space-y-4">
              <div class="space-y-1 w-full">
                <%= form.label :ref, :Reference, class: "input-label" %>
                <%= form.text_field :ref, placeholder: :reference, autofocus: true, class: "text-field input-md w-1/2" %>
                <% unless @service.errors[:ref].empty? %>
                  <div class="flex space-x-1 ">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-error" viewBox="0 0 20 20" fill="currentColor">
                      <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clip-rule="evenodd"/>
                    </svg>
                    <span class="txt_caption text-error ">The reference field <%= @service.errors[:ref].first %></span>
                  </div>
                <% end %>
              </div>
              <div class="space-y-1 w-full">
                <%= form.label :name, class: "input-label" %>
                <%= form.text_field :name, placeholder: :name, autofocus: true, class: "text-field input-md w-full" %>
                <% unless @service.errors[:name].empty? %>
                  <div class="flex space-x-1">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-error" viewBox="0 0 20 20" fill="currentColor">
                      <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clip-rule="evenodd"/>
                    </svg>
                    <span class="txt_caption text-error ">The first name field <%= @service.errors[:name].first %></span>
                  </div>
                <% end %>
              </div>
              <div class="space-y-1 w-full">
                <%= form.label :description, class: "input-label" %>
                <%= form.text_area :description, rows: 3, placeholder: :description, class: "text-field w-full" %>
                <% unless @service.errors[:description].empty? %>
                  <div class="flex space-x-1">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-error" viewBox="0 0 20 20" fill="currentColor">
                      <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clip-rule="evenodd"/>
                    </svg>
                    <span class="txt_caption text-error ">The last name
                        field <%= @service.errors[:description].first %></span>
                  </div>
                <% end %>
              </div>
            </div>
          </div>
        </div>
        <div class="w-full sticky bottom-0 flex items-center">
          <div class="mx-6 py-4 w-full h-full flex justify-end items-center">
            <%= form.submit "Save", class: "btn-primary btn-md", disabled: false %>
          </div>
        </div>
      <% end %>
    </div>
  </div>
<% end %>

services_controller.rb

def update
    respond_to do |format|
      if @service.update(service_params)
        flash.now[:success] = ["The Service " + @service.name + " was updated"]
        format.turbo_stream {
          render turbo_stream: [
            turbo_stream.update("flash", partial: "flash"), #Show Notification
            turbo_stream.update("show_service_" + @service.id.to_s, partial: "show", locals: { service: @service }),
            turbo_stream.update("edit_service_details"),
            turbo_stream.update("edit_service_categories"),
            turbo_stream.update("edit_service_variants"),
          ]
        }
      else
        flash.now[:error] = ["The Service " + @service.name + " had an error."]
        format.turbo_stream {
          render turbo_stream: [
            turbo_stream.update("flash", partial: "flash")
          ]
        }
      end
    end
  end

服务参数:

def service_params
    params.require(:service).permit(:name, :description, :ref, :state,
                                    service_per_categories_attributes: [:_destroy, :id, :service_category_id],
                                    service_variants_attributes: [
                                      :_destroy,
                                      :id,
                                      :name,
                                      :state,
                                      :price],
    )
  end

4

0 回答 0