简体   繁体   English

设计注销后删除方法返回错误

[英]Delete method returning error after devise logout

I'm running into a problem that I'm not exactly sure how to fix. 我遇到了一个我不确定如何解决的问题。

I have a simple to do list application with AJAX functionality on methods such as 'new', 'create', 'complete', 'delete', as well as Devise authentication. 我有一个简单的待办事项清单应用程序,它在诸如“新”,“创建”,“完成”,“删除”以及“设计”身份验证之类的方法上具有AJAX功能。

When I first enter a new session with a User, all of these methods work without a problem. 当我第一次与用户进入新会话时,所有这些方法都可以正常工作。 Additionally, the tasks are saved to only the user account, which is perfect. 此外,任务仅保存到用户帐户,这是完美的。

However, when I log out of an account, and then log back in, the delete method no longer works. 但是,当我注销一个帐户,然后重新登录时,删除方法不再起作用。 I receive the following error: 我收到以下错误:

ActiveRecord::RecordNotFound (Couldn't find Task with 'id'=)

My tasks_controller.rb is below: 我的tasks_controller.rb如下:

class TasksController < ApplicationController
  def index
    @task = current_user.tasks.all
  end
  def new
    @task = Task.new
    respond_to do |format|
      format.js
      format.html
    end
  end
  def create
    @task = current_user.tasks.new(task_params)
    @task.save
    respond_to do |format|
      format.html
      format.js
    end
  end
  def update
    @task = current_user.tasks.find(params[:id])
    @task.toggle :complete
    @task.save
    respond_to do |format|
      format.html
      format.js
    end
  end
  def destroy
    @task = Task.find(params[:id])
    @task.destroy
    respond_to do |format|
      format.js
      format.html
    end
  end
  private
  def task_params
    params.require(:task).permit(:id, :title, :complete)
  end
end

I'm not exactly sure how to fix this problem. 我不确定如何解决此问题。 Would anyone have an idea on what is going wrong here? 有人会对这里出什么问题有想法吗?

EDIT: 编辑:

I noticed that on my index page, I have a link to destroy the user's session at the top: 我注意到在索引页面上,我有一个链接可以在顶部破坏用户的会话:

<%= link_to "Log Out", destroy_user_session_path, :method => :delete %>

I'm wondering if rails is having some trouble with this as both the logout link and the delete link are referencing the same method. 我想知道Rails是否在此方面遇到麻烦,因为注销链接和删除链接都引用相同的方法。 If so, how can I change the name of the delete method for Task? 如果是这样,如何更改Task的delete方法的名称?

<div class="delete"><%= link_to "X", task_path(@task), method: :delete, remote: true %></div>

What is @task referencing? 什么是@task引用? It looks to me like you've set @task to a collection @task = current_user.tasks.all . 在我看来,您已经将@task设置为集合@task = current_user.tasks.all Which would be why your delete method can't find a specific record to delete. 这就是为什么您的删除方法找不到要删除的特定记录的原因。

-EDIT- -编辑-

Change @task in your index controller to @tasks as it is a collection. 将索引控制器中的@task更改为@tasks因为它是一个集合。 In your view, do something like: 在您看来,请执行以下操作:

<% @tasks.each do |task| %>
 <div><%= task.title %><div class="delete"><%= link_to "X", task_path(task), method: :delete, remote: true %></div></div>
<% end %>

The key here is that you have task_path(task) which is referencing a specific task id as opposed to task_path(@task) which is referencing a collection of tasks. 这里的关键是您有task_path(task)引用了特定的任务ID,而不是task_path(@task)引用了任务的集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM