Mika Andrianarijaona
Mika Andrianarijaona

Reputation: 1647

Rails 3 controller from plugin

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

Upvotes: 4

Views: 557

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107728

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.

Upvotes: 6

Related Questions