# Controller plugin?

**URL:** <https://www.ruby-forum.com/t/controller-plugin/117432>\
**Category:** Rails\
**Created:** [October 16, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/controller-plugin/117432 "2007-10-16T20:30:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tmac](https://www.ruby-forum.com/u/tmac)\
**Post date:** [October 16, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/controller-plugin/117432/1 "2007-10-16T20:30:13Z")

</div>

Do I understand correctly that you can’t write controller plugins?

Of course “can’t” is a big word, what I’m really wondering is what the  
major stumbling blocks to doing so are?

Am I missing something or wouldn’t this be extremely useful?

Thanks!  
Tim

---

<div class="post-metadata">

**Author:** ![tmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tmac](https://www.ruby-forum.com/u/tmac)\
**Post date:** [October 16, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/controller-plugin/117432/2 "2007-10-16T21:00:03Z")

</div>

I was reading this article:  
[http://nubyonrails.com/articles/2006/05/04/the-complete-guide-to-rails-plugins-part-i](http://nubyonrails.com/articles/2006/05/04/the-complete-guide-to-rails-plugins-part-i)

to quote:

“Controllers: You can’t directly write a controller plugin, but you  
can write a generator that copies a controller to your app/controllers  
directory. Intermediate difficulty.”

???

Tim

---

<div class="post-metadata">

**Author:** ![tmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tmac](https://www.ruby-forum.com/u/tmac)\
**Post date:** [October 16, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/controller-plugin/117432/3 "2007-10-16T20:52:27Z")

</div>

On 10/16/07, tmac [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Do I understand correctly that you can’t write controller plugins?
> 
> Of course “can’t” is a big word, what I’m really wondering is what the  
> major stumbling blocks to doing so are?
> 
> Am I missing something or wouldn’t this be extremely useful?

googling  
rails controller plugin

got me here  
[http://wiki.rubyonrails.org/rails/pages/Controller+Extensions](http://wiki.rubyonrails.org/rails/pages/Controller+Extensions)  
in short order.

Plugins are just code.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![tmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tmac](https://www.ruby-forum.com/u/tmac)\
**Post date:** [October 16, 2007, 9:47pm UTC](https://www.ruby-forum.com/t/controller-plugin/117432/4 "2007-10-16T21:47:25Z")

</div>

I think that’s pretty much the answer I was hoping for Jason! So the  
article is wrong and yes you can write controller plugins… thought  
that was odd!

I have a site/project where I use a controller base class that handles  
all the standard REST methods (new, show, edit, etc…) in a fairly  
generic way. It would certainly take some work but I thought it might  
be nice to share it as a plugin… at least for my own projects, and  
others if there is any interest.

I have looked a bit but haven’t seen anything like it. Is there a  
plugin that already does something similar?

Thanks a ton Jason!  
Tim

---

<div class="post-metadata">

**Author:** ![tmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tmac](https://www.ruby-forum.com/u/tmac)\
**Post date:** [October 17, 2007, 6:14am UTC](https://www.ruby-forum.com/t/controller-plugin/117432/5 "2007-10-17T06:14:09Z")

</div>

Hi Tim

Have you taken a lookat activescaffold ?  
Imanot sure towhat extentisit RESTful…

---

<div class="post-metadata">

**Author:** ![tmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tmac](https://www.ruby-forum.com/u/tmac)\
**Post date:** [October 16, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/controller-plugin/117432/6 "2007-10-16T21:28:41Z")

</div>

Outside of me being unable to really understand what “controller plugin”  
means in the quote, the article is from over a year ago.

If you’re trying to add macros or what not to controllers, you simply  
do:

ActionController::Base.send(:include, MyModuleOfMethods)

in your plugin/init.rb

What are you trying to do tmac?

Jason

---

<div class="post-metadata">

**Author:** ![tmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tmac](https://www.ruby-forum.com/u/tmac)\
**Post date:** [October 17, 2007, 10:59am UTC](https://www.ruby-forum.com/t/controller-plugin/117432/7 "2007-10-17T10:59:26Z")

</div>

On 16 Oct 2007, at 20:46, tmac wrote:

> I think that’s pretty much the answer I was hoping for Jason! So the  
> article is wrong and yes you can write controller plugins… thought  
> that was odd!

Yes and no. What the article is saying is that you can’t have  
foo\_controller in your plugin, install your plugin and then try to  
access [应用宝官网-全网最新最热手机应用游戏下载](http://myapp.com/foo)  
I think that there is stuff afoot so that you can specify the paths  
searched for views & controllers, but out of the box it won’t work.  
What you can of course do is a generator, provide modules for  
controllers or extend ActionController with macros and so on.

Fred

---

<div class="post-metadata">

**Author:** ![tmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tmac](https://www.ruby-forum.com/u/tmac)\
**Post date:** [October 19, 2007, 12:50am UTC](https://www.ruby-forum.com/t/controller-plugin/117432/8 "2007-10-19T00:50:25Z")

</div>

Thanks for setting me down the right path here guys… It appears to  
be relatively straight forward.

CHH - I’m only vaguely familiar with activescaffold … I was really  
trying to avoid a generator … unless I’m misunderstanding they  
typically generate a lot of very similar code over and over … this  
is for more than just admin stuff too:-)

I really just want to be able to inherit functionality into my  
controllers.

Tim

---

<div class="post-metadata">

**Author:** ![tmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tmac](https://www.ruby-forum.com/u/tmac)\
**Post date:** [October 17, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/controller-plugin/117432/9 "2007-10-17T15:57:52Z")

</div>

On 10/17/07, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> foo\_controller in your plugin, install your plugin and then try to  
> access [应用宝官网-全网最新最热手机应用游戏下载](http://myapp.com/foo)  
> I think that there is stuff afoot so that you can specify the paths  
> searched for views & controllers, but out of the box it won’t work.  
> What you can of course do is a generator, provide modules for  
> controllers or extend ActionController with macros and so on.

Hmmm, looks like you already can. Try adding your load paths to:

- MyController.template\_root
- ActionController::Routing.controller\_paths
- Dependencies.load\_paths

Demo:

$ cat vendor/plugins/test\_plugin/init.rb  
config.after\_initialize do  
path = File.expand\_path(‘app/controllers’, File.dirname( **FILE** ))  
ActionController::Routing.controller\_paths \<\< path  
Dependencies.load\_paths \<\< path  
end

$ cat  
vendor/plugins/test\_plugin/app/controllers/test\_plugin\_controller.rb  
class TestPluginController \< ApplicationController  
self.template\_root = File.expand\_path(‘…/views’,  
File.dirname( **FILE** ))  
def test  
end  
end

$ cat vendor/plugins/test\_plugin/app/views/test\_plugin/test.rhtml  
hi

$ script/console  
Loading development environment.

> > app.get(‘/test\_plugin/test’); puts app.response.body  
> > hi  
> > =\> nil
