ruby Ajax中不可处理的实体

yuvru6vn  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(74)

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

<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 %>

file routes.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

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

jgovgodb

jgovgodb1#

这段代码可能会引发一个错误,因为它没有使用StrongParameters
我建议将其改为:

def update_status
    @task = Task.find(params[:id])

    new_status = params.require(:task).permit(:status)

    @task.update(status: new_status)
  end

相关问题