简体   繁体   English

Rails - 如何避免必须从另一个控制器中的create action触发一个控制器中的create操作

[英]Rails - how do I avoid having to trigger the create action in one controller from the create action in another controller

Forgive a basic question here. 在这里原谅一个基本问题。

From reading around, I understand it's not best practice to trigger the create action of a controller from another - I'm looking for advice on how I should be organizing my code in the following situation: 通过阅读,我理解从另一个触发控制器的创建操作不是最佳实践 - 我正在寻找有关如何在以下情况下组织代码的建议:

I have two controllers: cases and notifications. 我有两个控制器:案例和通知。

When a new case is created I want it to also create a new notification. 创建新案例时,我希望它也创建新通知。

The notification create action creates a new instance in it's model, and sends an email. 通知创建操作在其模型中创建新实例,并发送电子邮件。

Am I right to think that I shouldn't just be calling Notification#create from my cases controller? 我是否认为我不应该只是从我的案件控制器调用Notification #create? Should I be extracting this to a helper or module? 我应该将其提取给帮助者或模块吗? If so, what would it look like? 如果是这样,它会是什么样子? The other roughly similar posts on this topic don't elaborate. 关于这个主题的其他大致相似的帖子没有详细说明。

Thank you. 谢谢。

Since create is a POST method, I don't think there is a way for calling create of notifications controller from cases controller. 由于create是POST方法,我认为没有办法从案例控制器调用通知控制器的创建。 My suggestion would be to move the creation of notification instance and the sending mail logic to the before save of your case model. 我的建议是将通知实例和发送邮件逻辑的创建移动到案例模型的保存之前。 This would create a notification each time a case is created and would also take care of the mail sending mechanism. 这将在每次创建案例时创建通知,并且还将处理邮件发送机制。 Since this is a part of your business requirement it is better to move the business logic into your model. 由于这是业务需求的一部分,因此最好将业务逻辑移到模型中。

This logic should be in your models. 这个逻辑应该在你的模型中。 Good solution is to use ActiveRecord callbacks for it. 好的解决方案是使用ActiveRecord回调。

# app/models/case.rb
class Case < ActiveRecord:Base
  after_create :create_notification

private        # <--- bottom of your model
  def create_notification
    Notification.create!(some_options_for_notification)
  end
end

# app/models/notification.rb
class Notification < ActiveRecord:Base
  after_create :send_notification

def send(opts)
  ...          # <--- some logic to send notification here
end


private        # <--- bottom of your model
  def send_notification
    send(some_options_for_sending)
  end
end

As one of options you can use rails observers for creating notification http://api.rubyonrails.org/classes/ActiveRecord/Observer.html 作为选项之一,您可以使用rails观察器来创建通知http://api.rubyonrails.org/classes/ActiveRecord/Observer.html

There are few differences between observers and callbacks, for example observers can't cancel any model action, but in your case I think there is no matter what to use. 观察者和回调之间几乎没有区别,例如观察者无法取消任何模型动作,但在您的情况下,我认为无论使用什么。

But observers also used to adhere to the Single Responsibility principle. 但观察者也习惯遵守单一责任原则。

Example

class CaseObserver < ActiveRecord::Observer
 def after_create(case)
    #create notification
 end
end

should be saved to /app/models/case_observer.rb. 应保存到/app/models/case_observer.rb.

in your config/application.rb 在你的config/application.rb

config.active_record.observers = :case_observer

You just need to write Notification.create in your case controllers action, that's it. 您只需要在案例控制器操作中编写Notification.create,就是这样。

You can write as much of code, create as much of model in your controller action as much you want. 您可以编写尽可能多的代码,在控制器操作中创建尽可能多的模型。

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

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