简体   繁体   中英

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.

My try was to search for railtie and views_path , since I am interested in writing a gem that shares views and helper files. How can I do that?

Where I can find documentation about this? what keyword should I google for: railtie, engine?

Gems like Devise use a feature available in Rails called 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.

If you want to get down to the nitty-gritty, a Rails Engine is just a special kind of Rack app. As such, it can be mounted by the Rails router . 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.


Make sure not to confuse this concept with what other gems like Omniauth use.

Such gems instead provide Rack Middleware , which doesn't necessarily need to be integrated into the app through the Rails router.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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