Rails 如何:如果项目有任务,则不应删除它:如何解决此问题?

发布于 2024-12-15 22:53:22 字数 383 浏览 0 评论 0原文

您好,我有一个项目,每个项目都有任务。任务属于项目。在删除项目之前,我想检查是否有相关任务。如果有任务我不想删除该项目。如果没有关联的任务,则应删除该项目。你能帮我看一下代码吗?我缺少什么?

class Project < ActiveRecord::Base  
  before_destroy :check_tasks    

  def check_tasks 
    if Project.find(params[:id]).tasks  
      flash[:notice] = 'This project has tasks.'
      redirect_to :action => 'list_projects'    
    end 
  end
end

Hi I have a project and each project has tasks. A task belongs to a project. Before I delete a project I want to check if there are related tasks. If there are tasks I don't want to delete the project. If there are no associated tasks, the project should be deleted. Can you please help me with the code? What am I missing?

class Project < ActiveRecord::Base  
  before_destroy :check_tasks    

  def check_tasks 
    if Project.find(params[:id]).tasks  
      flash[:notice] = 'This project has tasks.'
      redirect_to :action => 'list_projects'    
    end 
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绾颜 2024-12-22 22:53:22

从 before_destroy 方法返回 false 以防止实例被销毁。

该方法还应该返回一个有意义的错误以进行故障排除。

class Project < ActiveRecord::Base
  before_destroy :check_tasks

  def check_tasks
    if self.tasks.any?
      errors.add_to_base "Project has tasks and cannot be destroyed."
      return false
    end
  end
end

注意:flash[:notice] 和 params[:attr_name] 只能在控制器中使用。

Return false from the before_destroy method to prevent the instance from being destroyed.

The method should also return a meaningful error for troubleshooting.

class Project < ActiveRecord::Base
  before_destroy :check_tasks

  def check_tasks
    if self.tasks.any?
      errors.add_to_base "Project has tasks and cannot be destroyed."
      return false
    end
  end
end

Note: flash[:notice] and params[:attr_name] can only be used from controllers.

熟人话多 2024-12-22 22:53:22

你这里有几个问题。

  1. 您没有(或不应该)访问 params 变量(它仅在控制器和视图中可用,除非您将其传递给模型,这可能不是您想要的) 。
  2. 您的 if 检查 project.tasks 这是一个数组 - 即使是空数组也会评估为 true,因此您的其他代码分支永远不会发生项目是否有任务无关紧要。
  3. 您可能应该从 ProjectsController#destroy 操作中设置视图的错误消息,而不是在模型中。

解决方案:

  1. Project.find(params[:id]) 更改为 self - 您想要检查项目每个实例的任务。
  2. if 语句中的检查从 if self.tasks 更改为 if self.tasks.any? ,它会返回您想要的值 (false 如果数组为空,true 否则)。
  3. 将 flash[:notice] 从模型移至控制器,以及它们所属的redirect_to 调用。这意味着您的 check_tasks 方法可以更改为以下

代码:

def check_tasks
  return !self.tasks.any?
end

You have a couple of problems here.

  1. You don't (or shouldn't) have access to the params variable (it's available in controllers and views only, unless you're passing it to the model, which is probably not what you want).
  2. Your if checks against project.tasks which is an array - even an empty array evaluates to true, so your other code branch will never occur no matter if the project has tasks or not.
  3. You should probably be setting error messages for the view from your ProjectsController#destroy action, not in your model.

Solutions:

  1. Change Project.find(params[:id]) to self - you want to check the tasks for every instance of the Project.
  2. Change the check in your if statement from if self.tasks to if self.tasks.any? which returns the value you want (false if the array is empty, true otherwise).
  3. Move the flash[:notice] from your model to your controller, as well as the redirect_to call, where they belong. This means your check_tasks method can be changed to the following:

code:

def check_tasks
  return !self.tasks.any?
end
童话 2024-12-22 22:53:22

支票应该是self吗? (不确定您从哪里获取 params[:id] )。

不过还没有检查过 - 但由于我的用户模型需要类似的东西,我会看看它是如何工作的并回复你。

class Project < ActiveRecord::Base  
 before_destroy :check_tasks

 private

 def check_tasks
   #edited 
   if tasks.empty?  
     false
   end 
 end

Should the check be self instead? (not sure where you getting the params[:id] from).

Haven't checked this out yet though - but since I need something similar for my Users model I'll see how that works out and get back to you.

class Project < ActiveRecord::Base  
 before_destroy :check_tasks

 private

 def check_tasks
   #edited 
   if tasks.empty?  
     false
   end 
 end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文