Rails 3 controller from plugin

梦想与她 提交于 2020-01-03 16:01:49

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/7926115/rails-3-controller-from-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!