# Where to put code for all methods in a Controller

**URL:** <https://www.ruby-forum.com/t/where-to-put-code-for-all-methods-in-a-controller/122187>\
**Category:** Rails\
**Created:** [December 5, 2007, 11:33am UTC](https://www.ruby-forum.com/t/where-to-put-code-for-all-methods-in-a-controller/122187 "2007-12-05T11:33:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 5, 2007, 11:33am UTC](https://www.ruby-forum.com/t/where-to-put-code-for-all-methods-in-a-controller/122187/1 "2007-12-05T11:33:34Z")

</div>

Hello,

I’m using this in almost all methods of my Customer Controler

if params[:type]==‘in’  
# handle incoming customers  
kind = ::KIND\_CUSTOMER  
else  
# handle outgoing customers  
kind = ::KIND\_SUPPLIER  
end

I don’t want to copy&paste in all of the methods of this control, so I  
tried to put it at the top of the controller, to be available to all  
methods, but it doesn’t work …

## This is what I want, but it doesn’t work:

class CustomerController \< ApplicationController

around\_filter :login\_required

if params[:type]==‘in’  
# handle incoming customers  
kind = ::KIND\_CUSTOMER  
else  
# handle outgoing customers  
kind = ::KIND\_SUPPLIER  
end

erro =\> undefined local variable or method `params’ for  
CustomerController:Class

I tried to put in a helper, but the helper’s methods are not recognized  
in the controller …

thanks,

raimon

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 5, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/where-to-put-code-for-all-methods-in-a-controller/122187/2 "2007-12-05T12:39:02Z")

</div>

On 5 Dec 2007, at 10:33, Raimon Fs wrote:

> ```
> # handle outgoing customers
> kind = ::KIND_SUPPLIER
> 
> ```
> 
> end

add a before\_filter that does this (you’ll want to use an instance  
variable for kind instead of a local variable

Fred

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 5, 2007, 12:59pm UTC](https://www.ruby-forum.com/t/where-to-put-code-for-all-methods-in-a-controller/122187/3 "2007-12-05T12:59:07Z")

</div>

Frederick C. wrote:

> On 5 Dec 2007, at 10:33, Raimon Fs wrote:
> 
> > ```
> > # handle outgoing customers
> > kind = ::KIND_SUPPLIER
> > 
> > ```
> > 
> > end
> 
> add a before\_filter that does this (you’ll want to use an instance  
> variable for kind instead of a local variable
> 
> Fred

ok, I added this to the customer controll:

def get\_kind

```
if params[:type]=='in'
   # handle incoming customers
  @kind = ::KIND_CUSTOMER
else
   # handle outgoing customers
   @kind = ::KIND_SUPPLIER
end

```

end

and at the top, just:

before\_filter :get\_kind

and where I need it, I just put @kind

tahnks, it’s working, what I don’t understand, is why I have to create a  
method and call it with a before\_filter, instead of simply adding the  
code at the top …

regards,

raimon

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 5, 2007, 1:11pm UTC](https://www.ruby-forum.com/t/where-to-put-code-for-all-methods-in-a-controller/122187/4 "2007-12-05T13:11:58Z")

</div>

On 5 Dec 2007, at 11:59, Raimon Fs wrote:

> > variable for kind instead of a local variable  
> > else
> 
> and where I need it, I just put @kind
> 
> tahnks, it’s working, what I don’t understand, is why I have to  
> create a  
> method and call it with a before\_filter, instead of simply adding the  
> code at the top …

‘Adding code’ at the top tries to execute the code in the context of  
the controller class.  
Code added at the top level is run when the controller is loaded, ie  
once only - (in production classes aren’t reloaded). You need  
something that happens once per request, is run by an instance of the  
controller at a particular point in time (after the request has been  
received by the controller, before the action code is run), so it  
can’t possibly be achieved by class level stuff

Fred

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 5, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/where-to-put-code-for-all-methods-in-a-controller/122187/5 "2007-12-05T13:15:57Z")

</div>

Frederick C. wrote:

> ‘Adding code’ at the top tries to execute the code in the context of  
> the controller class.  
> Code added at the top level is run when the controller is loaded, ie  
> once only - (in production classes aren’t reloaded). You need  
> something that happens once per request, is run by an instance of the  
> controller at a particular point in time (after the request has been  
> received by the controller, before the action code is run), so it  
> can’t possibly be achieved by class level stuff
> 
> Fred

thanks, I understand now …

rai
