从 todoapp 中删除任务

deleting a task from todoapp

你好,你能解释一下我应该如何从待办事项应用程序中删除我的任务吗?

我的 show_html_erb 表单如下所示:

这是我学习的第三天ruby我很困惑我看到文档做了同样的事情但它只是不听我的命令:(

    <%= form_for([@todo_list, @todo_item]) do |f| %>

    <div class="input-group mb-4">
        <%= f.text_field :description , class:"form-control", placeholder:'Add todo items'%>
        <div class="input-group-append">
            <%= f.submit 'Add', class:"btn btn-primary input-group-btn"%>
        </div>
    </div>
    <%end%>

    <ul class='list-group'>
        <% @todo_list.todo_items.each do |todo_item| %>
        <% if todo_item.completed?%>
        <li class='list-group-item bg-light'>
            <div class="d-flex justify-content-between">
                <span class="text-muted">

                    <em> <%= todo_item.description%></em>




                </span>
                **<%= link_to 'DELETE',todo_list_path(todo_item),method: :delete, class:'btn btn-dark'%>**  ------>> **THISSSS**



                <%= link_to '#', class:'btn btn-dark' ,data:{reflex: 'click->TodoItem#mark_incomplete',id:todo_item.id} do%>
                <i class='fas fa-undo-alt'></i>
                <%end%>
            </div>



        </li>
        <%else%>
        <li class='list-group-item'>
            <div class="d-flex justify-content-between">
                <span>

                    <%= todo_item.description%>


                </span>
                <%=   link_to 'Destroy', todo_list_path(todo_item),
              method: :delete,
              data: { confirm: 'Are you sure?' } ,class:'btn btn-dark'%> ------>> THIIIISSSSS

                <%= link_to '#', class:'btn btn-info',data:{reflex: 'click->TodoItem#mark_complete',id:todo_item.id} do %>
                <i class='fas fa-check'></i>
                <%end%>
            </div>



        </li>

        <%end%>


        <%end%>



    </ul>
</div>

我错了什么,(我想是路径,当我删除时)

我的 todolist 控制器看起来像这样:

class TodoListsController < ApplicationController
  before_action :set_todo_list, only: [:show, :edit, :update, :destroy]

  # GET /todo_lists
  # GET /todo_lists.json
  def index
    @todo_lists = TodoList.all
  end

  # GET /todo_lists/1
  # GET /todo_lists/1.json
  def show
    @todo_item = TodoItem.new
  end

  # GET /todo_lists/new
  def new
    @todo_list = TodoList.new
  end

  # GET /todo_lists/1/edit
  def edit
  end

  # POST /todo_lists
  # POST /todo_lists.json
  def create
    @todo_list = TodoList.new(todo_list_params)

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

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

  # DELETE /todo_lists/1
  # DELETE /todo_lists/1.json
  def destroy
    @todo_list = TodoList.find(params[:id])
    @todo_list.destroy
    respond_to do |format|
      format.html { redirect_to todo_lists_url, notice: 'Todo list was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Only allow a list of trusted parameters through.
    def todo_list_params
      params.require(:todo_list).permit(:title)
    end
end

问题是您没有将正确的 object/path 传递给 delete。试试下面的代码。

<%=   link_to 'Destroy', todo_item,
              method: :delete,
              data: { confirm: 'Are you sure?' } ,class:'btn btn-dark'%>

我相信你上面分享的 htmlindex.html.erb 而不是 show.html.erb

所以我找到了答案,我所要做的就是像这样链接删除方法:

 <%=   link_to 'Destroy', todo_list_todo_item_path(@todo_list,todo_item),
              method: :delete,
              data: { confirm: 'Are you sure?' } ,class:'btn btn-dark'%>

这解决了我的问题(原因是因为正如您在我的 show.html.erb 文件中看到的那样,我使用了嵌套脚手架)