简体   繁体   English

扩展Rails引擎功能

[英]Extending Rails Engine Functionality

I am a beginner to RoR and could use some guidance. 我是RoR的初学者,可以使用一些指导。 I have been trolling this stackoverflow answer religiously these past few days. 过去几天,我一直在认真地讨论这个stackoverflow答案。

Rails engines extending functionality Rails引擎扩展功能

But the error that keeps being thrown is: 'undefined method join' 但是不断抛出的错误是: 'undefined method join'

I have an abstract model called OrderError. 我有一个名为OrderError的抽象模型。 OrderErrorDetails is my other model , which is used to actually store data within the database. OrderErrorDetails是我的另一个模型,用于在数据库中实际存储数据。 The OrderError abstract model is here solely to provide functionality, not to store anything. 这里的OrderError抽象模型仅用于提供功能,不存储任何内容。 :

module MyEngine
  class OrderError
    self.abstract_class = true
    has_one :order_error_detail

    def self.new
      #my_code
    end

  end
end

So what I tried instead of requiring (stack overflow link above) was placing the following code to my app's Order Model: 因此,我尝试而不是要求进行操作(上面的堆栈溢出链接)是将以下代码放入我的应用程序的“订单模型”中:

order_error MyEngine::OrderError.new

When I did this, I was able to successfully access my engines OrderError model. 当我这样做时,我能够成功访问引擎OrderError模型。 However, the error I now get is: The method .order() must contain arguments. 但是,我现在得到的错误是: 方法.order()必须包含参数。

My hope is that once the engine is mounted into an app, I can call my engines various methods. 我的希望是,一旦将引擎安装到应用程序中,就可以使用多种方法调用引擎。 This will provide the user of the engine some default functionality. 这将为引擎用户提供一些默认功能。

Should I be using abstract classes to begin with? 我应该从抽象类开始吗? In my mind the answer is yes, because my abstract model's only purpose is to provide methods that can be used. 在我看来,答案是肯定的,因为我的抽象模型的唯一目的是提供可以使用的方法。 If so, how does one efficiently provide access to those methods? 如果是这样,一个人如何有效地提供对这些方法的访问?

using: 使用:

MyEngine::OrderError.new 

is the correct way to extend engine model functionality. 是扩展引擎模型功能的正确方法。 In the above example, the method "new" had some problems with the way it was structured. 在上面的示例中,“ new”方法的结构存在一些问题。 This comes down to engine namespacing. 这归结为引擎命名空间。 Every class is encapsulated in the module of MyEngine (The engine name). 每个类都封装在MyEngine的模块(引擎名称)中。 This is so that your engine code is modular and seperate from its parent app code. 因此,您的引擎代码是模块化的,并且与其父应用程序代码分开。 The best explanation for namespacing was found on the getting started with engines guide here: http://guides.rubyonrails.org/v3.2.13/engines.html#what-are-engines 有关命名空间的最佳说明,请参见此处的《引擎入门指南》: http : //guides.rubyonrails.org/v3.2.13/engines.html#what-are-engines

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

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