【问题标题】:Can't multiple destroy with checkboxes不能用复选框多次销毁
【发布时间】:2017-12-22 12:20:03
【问题描述】:

我需要使用复选框删除多个任务,当我这样做时出现错误 每个任务都有任务和复选框,当单击复选框然后单击“删除选定”按钮时,必须删除所有选中的任务

TasksController 中的 NoMethodError#delete_multiple

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

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

和 我的任务控制器

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

我的任务.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

为什么它不能 undefined 方法 `destroy' ? 谁能帮帮我?

【问题讨论】:

  • 让我们试试下面给定的解决方案,让我知道以获得进一步的指导。

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


【解决方案1】:

问题在下面一行

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

你可以像下面这样修改

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

我认为会有所帮助

【讨论】:

  • def destroy Task.find(params[:cb_tasks]).destroy flash[:success] = "Material destroyed." 中未定义的局部变量或方法 `params'。 redirect_to tasks_url 结束
  • @asda111sd 正如我在下面解释的那样,您的模型方法destroy() 不知道params[:cb_tasks],因此它将无法在此处执行。
  • TasksController#delete_multiple 中的 NameError \n #<0x007fb3e079d268>
【解决方案2】:

而不是在模型端覆盖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

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      相关资源
      最近更新 更多