# Global functions and variables

**URL:** <https://www.ruby-forum.com/t/global-functions-and-variables/51559>\
**Category:** Rails\
**Created:** [January 3, 2006, 7:31am UTC](https://www.ruby-forum.com/t/global-functions-and-variables/51559 "2006-01-03T07:31:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nodrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nodrew](https://www.ruby-forum.com/u/nodrew)\
**Post date:** [January 3, 2006, 7:31am UTC](https://www.ruby-forum.com/t/global-functions-and-variables/51559/1 "2006-01-03T07:31:26Z")

</div>

Hey People,

I’m fairly new to Rails and have perhaps an obvious question. I would  
like  
to know if there is a place that I can declare variables and methods  
that  
are globally available to all controllers. My main reason for this is I  
like  
to auto generate select boxes based off of the contents of a hash and  
would  
like to be able to do this in all views in all controllers, so that I do  
not  
have to repeat either the methods involved or the hashes I’ve created.

Would I accomplish this by just creating a new .rb file and requiring it  
in  
the environments file, or is there a more standard way to do this. I  
know  
there are helpers available, but wasn’t sure if they worked globally or  
just  
for certain views.

Thanks,  
~Drew

---

<div class="post-metadata">

**Author:** ![nodrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nodrew](https://www.ruby-forum.com/u/nodrew)\
**Post date:** [January 3, 2006, 9:33am UTC](https://www.ruby-forum.com/t/global-functions-and-variables/51559/2 "2006-01-03T09:33:03Z")

</div>

Excellant, that was exactly what I was looking for. Thanks.

---

<div class="post-metadata">

**Author:** ![nodrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nodrew](https://www.ruby-forum.com/u/nodrew)\
**Post date:** [January 3, 2006, 9:14am UTC](https://www.ruby-forum.com/t/global-functions-and-variables/51559/3 "2006-01-03T09:14:52Z")

</div>

On Tuesday 03 Jan 2006 06:31, Drew B. wrote:

> there are helpers available, but wasn’t sure if they worked globally or  
> just for certain views.

Your application\_helper.rb works globally, or for more specific things,  
you  
can put them in the helpers for your individual classes. All are found  
in  
your helpers directory.

So for example, I have a boolean select helper in my application.rb to  
give  
yes/no rather than true/false or a checkbox, since my customer would  
prefer  
yes/no to true/false throughout the site:

def boolean\_select(object, fieldname, options ={})  
select(object, fieldname, {“Yes” =\> true, “No” =\> false}, options)  
end

But then in my booking\_helper.rb, I have a title box helper, since that  
one  
only applies to bookings:

def title\_select(object, fieldname, options ={})  
select(object, fieldname, %w{Mr Mrs Miss Ms Dr Prof}, options)  
end

I’ve included those two examples because they’re hopefully similar to  
whatever  
you’re trying to do.

For non-view related methods (I have some that clean up strings, used by  
multiple models and so on), you can put things in application.rb, or if  
it  
makes sense to (perhaps you have a lot of related stuff that deserves  
it’s  
own file), just make another .rb file and include it in application.rb.

As for global variables, I’m not sure, since I’m not using any, but I  
guess  
you might as well just try putting them in application.rb as well.

There’s also this nice article by Amy Hoy, which might help you:  
[http://www.slash7.com/articles/2005/03/06/rails-what-goes-where](http://www.slash7.com/articles/2005/03/06/rails-what-goes-where)

~Dave

–

Dave S.  
Rent-A-Monkey Website Development  
Web: [http://www.rentamonkey.com/](http://www.rentamonkey.com/)

---

<div class="post-metadata">

**Author:** ![nodrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nodrew](https://www.ruby-forum.com/u/nodrew)\
**Post date:** [January 3, 2006, 10:06am UTC](https://www.ruby-forum.com/t/global-functions-and-variables/51559/4 "2006-01-03T10:06:32Z")

</div>

On Tuesday 03 Jan 2006 08:11, Dave S. wrote:

> So for example, I have a boolean select helper in my application.rb to give  
> yes/no rather than true/false or a checkbox, since my customer would prefer  
> yes/no to true/false throughout the site:

Sorry, my bad, that was in application\_helper.rb, not application.rb!!

~Dave

–

Dave S.  
Rent-A-Monkey Website Development  
Web: [http://www.rentamonkey.com/](http://www.rentamonkey.com/)
