来自插件的 Rails 3 控制器

发布于 2024-12-12 12:25:36 字数 240 浏览 1 评论 0原文

我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2024-12-19 12:25:36

您需要在插件中定义一个继承自 Rails::Engine 的类。实际上,您想要的功能是一个引擎

像这样定义类:

lib/your_thing/engine.rb

module YourThing
  class Engine < Rails::Engine
  end
end

然后,您可以在该插件中的 app/controllers 中定义引擎的控制器,为了使它们能够整齐地工作,您还可以需要为它们定义路由,您可以在引擎内的 config/routes.rb 中执行此操作,如下所示:

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

接下来,您需要将引擎安装在应用程序中:

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

然后应用程序将能够使用引擎中的路线。

有关更多信息,我正在编写官方 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

module YourThing
  class Engine < Rails::Engine
  end
end

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文