代码之家  ›  专栏  ›  技术社区  ›  Alex Nunes

Ajax中不可处理的实体

  •  0
  • Alex Nunes  · 技术社区  · 2 年前

    我需要发出ajax请求来更改“任务”对象的“状态”参数。 脚本,没错,我可以得到任务的id和新的状态,这就是我所需要的。然而,我相信错误在routes.rb文件中,或者在我放入控制器的update_status函数中,或者甚至在ajax url中。它给出以下错误:jquery-1.12.4.js:10254 PATCH http://127.0.0.1:3000/tasks/13/update_status 422(不可处理实体)

    js和ajax在index.html.erb中:

        <script>
        $(function() {
          var taskId;
          $(".drag").draggable({
            revert: "invalid",
            start: function(event, ui) {
              // Stores the task ID when the drag starts
              taskId = ui.helper.data("task-id");
            }
          });
        
          $(".box").droppable({
            accept: ".drag",
            drop: function(event, ui) {
              // When a child div is dropped onto a parent div
              $(this).append(ui.helper); // Move a div filha para a div pai
        
        
              // Get the new status based on the parent div
              var newStatus = $(this).attr("id");
              // Simulate an AJAX request to update task status
              console.log("Tarefa " + taskId + " movida para " + newStatus);
        
              $.ajax({
              url: "/tasks/" + taskId + "/update_status",
              method: "PATCH", 
              data: { task: { status: newStatus } },
              success: function(response) {
                  console.log(response);
              }
        
            });
            }
          });
          });
        </script>
        <%= link_to "New task", new_task_path %>
    

    文件路由.db:

        Rails.application.routes.draw do
          resources :tasks do
            member do
              patch 'update_status' # Nome da rota personalizada
            end
          end 
          root to: "static_pages#index"
        end
    

    tasks_controller.rb文件的一部分:

        def update
            respond_to do |format|
              if @task.update(task_params)
                format.html { redirect_to task_url(@task), notice: "Task was successfully updated." }
                format.json { render :show, status: :ok, location: @task }
              else
                format.html { render :edit, status: :unprocessable_entity }
                format.json { render json: @task.errors, status: :unprocessable_entity }
              end
            end
          end
        
          def update_status
            @task = Task.find(params[:id])
        
            # Verifique se o status fornecido é válido (você pode adicionar suas próprias validações aqui)
            new_status = params[:status]
        
            @task.update(status: new_status)
        
          end
    

    我试图用ajax更改状态,结果得到了一个无法处理的实体

    0 回复  |  直到 2 年前
        1
  •  1
  •   spickermann    2 年前

    该代码可能会引发错误,因为它没有使用 StrongParameters .

    我建议将其更改为:

      def update_status
        @task = Task.find(params[:id])
    
        new_status = params.require(:task).permit(:status)
    
        @task.update(status: new_status)
      end
    
    推荐文章