来自插件的 Rails 3 控制器
我正在创建一个 Rails 3 插件,我想在其中集成控制器,rails 将自动将其视为 app/controllers 文件夹中的“普通”控制器。我该如何做到这一点,或者从插件获得自定义控制器的最佳解决方案是什么? 我从 guides.rubyonrails.org 找到了文档,但他们更改了文档,并且插件开发不再包含控制器。
谢谢
I am creating a Rails 3 plugin and I want to integrate controllers in it that will be automaticly consider by rails as a "normal" controller from the app/controllers folder. How can I do that or what is the best solution for me to have custom controllers from a plugin?
I have found documentations from guides.rubyonrails.org but they have changed the documentation and plugin development doesn't include controllers anymore.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在插件中定义一个继承自
Rails::Engine
的类。实际上,您想要的功能是一个引擎。像这样定义类:
lib/your_thing/engine.rb
然后,您可以在该插件中的
app/controllers
中定义引擎的控制器,为了使它们能够整齐地工作,您还可以需要为它们定义路由,您可以在引擎内的config/routes.rb
中执行此操作,如下所示:接下来,您需要将引擎安装在应用程序中:
然后应用程序将能够使用引擎中的路线。
有关更多信息,我正在编写官方 Rails Engine 指南,您可以在此处参考。请让我如果您还有其他问题,我会尽力在指南中回答。
You will need to define a class within your plugin that inherits from
Rails::Engine
. In effect, the feature you want is an engine.Define the class like this:
lib/your_thing/engine.rb
You can then define your engine's controllers at
app/controllers
within that plugin and for them to work neatly you will also need to define routes for them, which you can do insideconfig/routes.rb
inside the engine like this:Next, you'll need to mount your engine inside your application:
The application then will be able to use routes from your engine.
For more information, I'm in the progress of writing the official Rails Engine guide which you can reference here. Please let me know if you have any further questions and I'll try to answer them in the guide.