Rails 3.1:引擎与可挂载应用 [英] Rails 3.1: Engine vs. Mountable App

查看:51
本文介绍了Rails 3.1:引擎与可挂载应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我理解 Rails Engine 和 Mountable 应用之间的区别?在 Rails 3.1 中,您可以使用rails new plugin ___"命令创建任一插件.

Can someone help me understand the differences between a Rails Engine and a Mountable app? In Rails 3.1, you can create either one with the "rails new plugin ___" command.

rails plugin new forum --full        # Engine
rails plugin new forum --mountable   # Mountable App

你想在什么时候使用一个和另一个?我知道您可以将 Engine 打包为 gem.可挂载应用程序不是这种情况吗?还有哪些不同之处?

When would you want to use one versus the other? I know you can package an Engine as a gem, for one. Is that not the case for Mountable Apps? What other differences are there?

推荐答案

我注意到以下几点:

完整引擎

对于完整的引擎,父应用程序从引擎继承路由.无需在 parent_app/config/routes.rb 中指定任何内容.在 Gemfile 中指定 gem 足以让父应用继承模型、路由等.引擎路由指定为:

With a full engine, the parent application inherits the routes from the engine. It is not necessary to specify anything in parent_app/config/routes.rb. Specifying the gem in Gemfile is enough for the parent app to inherit the models, routes etc. The engine routes are specified as:

# my_engine/config/routes.rb 
Rails.application.routes.draw do 
  # whatever 
end 

没有模型、控制器等的命名空间.这些是立即父应用程序可以访问.

No namespacing of models, controllers, etc. These are immediately accessible to the parent application.

可安装引擎

引擎的命名空间默认是隔离的:

The engine's namespace is isolated by default:

# my_engine/lib/my_engine/engine.rb
module MyEngine 
  class Engine < Rails::Engine 
    isolate_namespace MyEngine 
  end 
end

使用可挂载引擎,路由是命名空间的,父应用程序可以将此功能捆绑在单个路由下:

With a mountable engine, the routes are namespaced and the parent app can bundle this functionality under a single route:

# my_engine/config/routes.rb 
MyEngine::Engine.routes.draw do 
  #whatever 
end 

# parent_app/config/routes.rb 
ParentApp::Application.routes.draw do 
    mount MyEngine::Engine => "/engine", :as => "namespaced" 
end 

模型、控制器等与父应用程序隔离 - 尽管可以轻松共享助手.

Models, controllers, etc are isolated from the parent application - although helpers can be shared easily.

这些是我发现的主要差异.也许还有其他人?我已在此处询问,但尚未收到回复.

These are the main differences I have spotted. Perhaps there are others? I have asked over here, but have yet to receive a response.

我的印象是,由于完整引擎不会将自身与父应用程序隔离,因此最好将其用作与父应用程序相邻的独立应用程序.我相信可能会发生名称冲突.

My impression is that since a full engine does not isolate itself from the parent application, it is best used as a standalone application adjacent to the parent app. I believe name clashes could occur.

在您希望避免名称冲突并将引擎捆绑在父应用程序中的特定路径下的情况下,可以使用可安装的引擎.例如,我正在构建我的第一个专为客户服务设计的引擎.父应用程序可以将其功能捆绑在单个路由下,例如:

A mountable engine could be used in situations where you want to avoid name conflicts and bundle the engine under one specific route in the parent application. For example, I am working on building my first engine designed for customer service. The parent application could bundle it's functionality under a single route such as:

mount Cornerstone::Engine => "/cornerstone", :as => "help" 

如果我的假设偏离了我的假设,请有人告诉我,我会修复此回复.我写了一篇关于这个主题的小文章这里 干杯!

If I'm way off in my assumptions, someone please let me know and I'll fix this response. I have made a small article about the subject here Cheers!

这篇关于Rails 3.1:引擎与可挂载应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆