无法使用复选框进行多次销毁

时间:2017-12-22 12:20:04

标签: ruby-on-rails ruby model-view-controller

我需要使用复选框多次删除任务,当我这样做时会出错 为每个任务创建任务和复选框,单击复选框,然后单击"删除所选"按钮必须删除所有已检查的任务

  

TasksController中的NoMethodError#delete_multiple

未定义的方法`destroy'
这是我的要求

{"utf8"=>"✓",
 "_method"=>"delete",
 "authenticity_token"=>"Bc2lZKUDVOjkQ0DYTDNI8TVliMaDKb+z2wz46RJeFqFol8WyEABA8sAz+WPCQOD2V0SEyqSHAryuoYQ6nvk4sA==",
 "cb_tasks"=>["1", "3", "4"],
 "commit"=>"Delete selected"} 

和 我的task_controller

class TasksController < ApplicationController
  before_action :set_task, only: [:show, :edit, :update, :destroy]

  # GET /tasks
  # GET /tasks.json
  def index
    @tasks = Task.all
  end

  # GET /tasks/1
  # GET /tasks/1.json
  def show
  end

  # GET /tasks/new
  def new
    @task = Task.new
  end

  # GET /tasks/1/edit
  def edit
  end

  # POST /tasks
  # POST /tasks.json
  def create
    @task = Task.new(task_params)

    respond_to do |format|
      if @task.save
        format.html { redirect_to @task, notice: 'Task was successfully created.' }
        format.json { render :show, status: :created, location: @task }
      else
        format.html { render :new }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /tasks/1
  # PATCH/PUT /tasks/1.json
  def update
    respond_to do |format|
      if @task.update(task_params)
        format.html { redirect_to @task, notice: 'Task was successfully updated.' }
        format.json { render :show, status: :ok, location: @task }
      else
        format.html { render :edit }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tasks/1
  # DELETE /tasks/1.json
  def destroy
    @task.destroy
    respond_to do |format|
      format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  # multiple delete with checkboxes
  def delete_multiple
    @tasks = Task.find(params[:cb_tasks])
    @tasks.destroy() // **here is a problem**
    respond_to do |format|
      format.html { redirect_to tasks_url, notice: 'Tasks was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_task
      @task = Task.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def task_params
      params.require(:task).permit(:title, :description, :priority, :due, :done)
    end
end

my task.rb

class Task < ApplicationRecord

  def destroy
    Task.find(params[:cb_tasks]).destroy
    flash[:success] = "Material destroyed."
    redirect_to tasks_url
  end
end

我的index.html.rb

<%= form_tag delete_multiple_tasks_path, method: :delete do %>
  <div class="CSSTableGenerator" >
    <table >
      <tr>
        <td>Tasks</td>
      </tr>
      <% @tasks.each do |task| %>

        <tr>
          <td><%= check_box_tag "cb_tasks[]", task.id %></td>
          <td><%= link_to task.title, task %></td>-->
          <td><%= link_to 'Edit', edit_task_path(task) %></td>
          <td><%= link_to 'Destroy', task, method: :delete, data: {confirm: 'Are you sure?'} %></td>
        </tr>
      <% end %>
    </table>
  </div>
  <%= submit_tag "Delete selected" %>
<% end %>

我的路线

  resources :tasks do
    collection do
      delete 'delete_multiple'
    end
  end

为什么它不能定义方法`destroy&#39; ? 任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:2)

问题出在

之下
@tasks = Task.find(params[:cb_tasks])
@tasks.destroy() // **here is a problem**

你可以修改如下

Task.where(id: params[:cb_tasks]).destroy_all

我认为会有所帮助

答案 1 :(得分:0)

而不是在模型方面覆盖destroy方法你可以这样做,我建议你不要覆盖destroy但是如果你覆盖destroy方法而不是你没有将params[:cb_tasks]传递给它方法并在Task.find(params[:cb_tasks]).destroy中,此行无法执行,因为它在模型中未获得params[:cb_tasks] 所以你可以这样做 -

  def delete_multiple
    @tasks = Task.find(params[:cb_tasks])
    @tasks.destroy_all // **replace it**
    respond_to do |format|
      format.html { redirect_to tasks_url, notice: 'Tasks was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

答案 2 :(得分:0)

我发现它是如何运作的。我删除了方法破坏了模型 并在控制器中执行此操作

def delete_multiple
  Task.where(id: params[:cb_tasks]).destroy_all
end

感谢您的所有答案,您是最棒的!