来自插件的Rails 3控制器 [英] Rails 3 controller from plugin

查看:73
本文介绍了来自插件的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.

谢谢

推荐答案

您将需要在插件中定义一个继承自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.

像这样定义类:

lib/your_thing/engine.rb

module YourThing
  class Engine < Rails::Engine
  end
end

然后,您可以在该插件内的app/controllers处定义引擎的控制器,并使它们整洁地工作,还需要为其定义路由,您可以在引擎内的config/routes.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 inside config/routes.rb inside the engine like this:

YourThing::Engine.routes.draw do
  resources :things
end

下一步,您需要在应用程序中挂载引擎:

Next, you'll need to mount your engine inside your application:

mount YourThing::Engine, :at => "/"

然后该应用程序将能够使用您引擎中的路由.

The application then will be able to use routes from your engine.

有关更多信息,我正在编写正式的Rails 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.

这篇关于来自插件的Rails 3控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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