简体   繁体   English

devise gem如何使它的应用程序文件夹可用于Rails?

[英]How does devise gem make its app folder available to Rails?

I see their app folder but I can't see how is this folder made available to Rails. 我看到了他们的app文件夹,但看不到Rails如何使用该文件夹。

My try was to search for railtie and views_path , since I am interested in writing a gem that shares views and helper files. 我的尝试是搜索railtieviews_path ,因为我有兴趣编写共享视图和帮助文件的gem。 How can I do that? 我怎样才能做到这一点?

Where I can find documentation about this? 在哪里可以找到有关此文档的信息? what keyword should I google for: railtie, engine? 我应该在Google上搜索哪个关键字:railtie,engine?

Gems like Devise use a feature available in Rails called Engines. 像Devise之类的宝石使用Rails中可用的一项称为Engines的功能。

Engines basically allow you to build your own pieces of functionality and seamlessly plug them into another application, effectively making the views, models and controllers in your engine available as if they were a part of it. 引擎基本上允许您构建自己的功能并将它们无缝地插入另一个应用程序,从而有效地使引擎中的视图,模型和控制器可用,就好像它们是其中的一部分一样。

Rails Engines are not only a good way to provide functionality in gems, but can also be good to keep your own apps tidy by grouping together various pieces of functionality. Rails引擎不仅是在gems中提供功能的好方法,而且通过将各种功能组合在一起 ,可以使自己的应用保持整洁

If you want to get down to the nitty-gritty, a Rails Engine is just a special kind of Rack app. 如果您想了解细节,Rails Engine只是一种特殊的Rack应用程序。 As such, it can be mounted by the Rails router . 这样,它可以由Rails路由器安装 In simple terms, the Rails router detects requests to the paths these apps have been "mounted" on, and routes them to the controllers made available by the engine instead of your normal controllers. 简而言之,Rails路由器会检测对这些应用已“安装”到的路径的请求,并将其路由到引擎提供的控制器(而不是常规控制器)。


Make sure not to confuse this concept with what other gems like Omniauth use. 确保不要将此概念与Omniauth等其他宝石混淆。

Such gems instead provide Rack Middleware , which doesn't necessarily need to be integrated into the app through the Rails router. 此类宝石提供了Rack Middleware ,它不一定需要通过Rails路由器集成到应用程序中。

You need to learn a bit more about Rack to understand how this works exactly, but in essence every request made to the application passes through Middleware like Omniauth before actually hitting the Rails router. 您需要多了解一些有关Rack的知识,以了解其工作原理,但实际上,对应用程序的每个请求都必须先通过Omniware(如Omniauth)进行传递,然后才能真正到达Rails路由器。 Said middleware can either respond to the request by itself (basically making it stop and never reach the router and rest of the application), or "augment" the request (for example, Omniauth checks and performs authentication and then stores the data in the request env ) pass on the request to the next Middleware. 所述中间件可以自己响应请求(基本上使其停止,并且永远不会到达路由器和应用程序的其余部分),或者“增强”请求(例如,Omniauth检查并执行身份验证,然后将数据存储在请求中env )将请求传递给下一个中间件。

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

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