# DRY question

**URL:** <https://www.ruby-forum.com/t/dry-question/184277>\
**Category:** Rails\
**Created:** [February 28, 2010, 12:09am UTC](https://www.ruby-forum.com/t/dry-question/184277 "2010-02-28T00:09:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [February 28, 2010, 12:09am UTC](https://www.ruby-forum.com/t/dry-question/184277/1 "2010-02-28T00:09:05Z")

</div>

I need to have the exact same method available in all controllers and  
in all views. Right now, I have it repeated in app/controllers/  
application\_controller.rb and app/helpers/application\_helper.rb. In  
DRY tradition, what’s the best way to only have it in one place?  
TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [February 28, 2010, 12:12am UTC](https://www.ruby-forum.com/t/dry-question/184277/2 "2010-02-28T00:12:36Z")

</div>

Just put it in a helper and include that helper in ApplicationController  
like this

include MyHelperWithGlobalStuffInIt

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [February 28, 2010, 1:39am UTC](https://www.ruby-forum.com/t/dry-question/184277/3 "2010-02-28T01:39:48Z")

</div>

On Feb 27, 5:10 pm, ben wiseley [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just put it in a helper and include that helper in ApplicationController  
> like this
> 
> include MyHelperWithGlobalStuffInIt

You’re right, putting

include ApplicationHelper

into application\_controller.rb does the trick.

But wasn’t that what

helper :all # include all helpers, all the time

was supposed to do?

Or do I misunderstand?

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [February 28, 2010, 6:17am UTC](https://www.ruby-forum.com/t/dry-question/184277/4 "2010-02-28T06:17:49Z")

</div>

I’ve wondered about that too. That doesn’t work for me either. It  
doesn’t  
raise an error but doesn’t include :all either.

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [February 28, 2010, 1:26pm UTC](https://www.ruby-forum.com/t/dry-question/184277/5 "2010-02-28T13:26:45Z")

</div>

On Feb 28, 12:38 am, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But wasn’t that what
> 
> helper :all # include all helpers, all the time
> 
> was supposed to do?
> 
> Or do I misunderstand?

You do :-). The helper method controls which of your helpers (in app/  
helpers) are available to the views for that controller. Without the  
helper :all call, by default views rendered by foo\_controller will get  
helper methods from foo\_helper. You can name specific modules, so for  
example if you a bunch of common helpers in app/helpers/  
common\_helpers.rb then you say helper :common\_helpers or helper  
CommonHelpers to add that module for the controller in question.

helper :all means add all of the helpers from app/helpers to all of  
your views.

Another way of solving the original problem is declaring the method in  
application\_controller and then saying

helper\_method :some\_helper

which takes the named controller method and makes it into a view  
helper.

Fred

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [February 28, 2010, 2:48pm UTC](https://www.ruby-forum.com/t/dry-question/184277/6 "2010-02-28T14:48:43Z")

</div>

> > Or do I misunderstand?
> 
> You do :-). The helper method controls which of your helpers (in app/  
> helpers) are available to the views for that controller.

Ahhhhh. So _that’s_ what it’s there for! 🙂

> Another way of solving the original problem is declaring the method in  
> application\_controller and then saying
> 
> helper\_method :some\_helper
> 
> which takes the named controller method and makes it into a view  
> helper.

Thanks!  
Craig
