# Method Common to Several Actions

**URL:** <https://www.ruby-forum.com/t/method-common-to-several-actions/140490>\
**Category:** Rails\
**Created:** [June 14, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/method-common-to-several-actions/140490 "2008-06-14T22:32:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [June 14, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/method-common-to-several-actions/140490/1 "2008-06-14T22:32:44Z")

</div>

If one wanted to write a method to be used by several different  
actions, where would that method be placed? Thanks for any input.

```
     ... doug
```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [June 14, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/method-common-to-several-actions/140490/2 "2008-06-14T23:45:35Z")

</div>

you can put it in application.rb

depending on what the method does, you may instead want to put it in a  
library, or in a model.

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [June 15, 2008, 3:02am UTC](https://www.ruby-forum.com/t/method-common-to-several-actions/140490/3 "2008-06-15T03:02:31Z")

</div>

> you can put it in application.rb
> 
> depending on what the method does, you may instead want to put it in a  
> library, or in a model.

Good suggestions. The thing about application.rb is that I’m thinking  
that it would be nice if I reserved that for methods that applied to  
more than one controller. Probably the same philosophy would apply to  
a library although I could make separate libraries for each controller  
for which I was providing some sort of special method. That would  
seem like a good solution. Placing it in a model would also seem like  
a really good choice. Frankly, I’ve never understood how one knows  
exactly what should go into a model. I know that (for whatever  
reason) I always use a model when I’m connecting to a database or  
using Action Mailer. Your suggestion of using the model in this case  
strikes me as a very good one. I just wish I understood exactly WHY  
it’s a good idea. Thanks for the input.

```
      ... doug
```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [June 15, 2008, 4:27am UTC](https://www.ruby-forum.com/t/method-common-to-several-actions/140490/4 "2008-06-15T04:27:51Z")

</div>

> If one wanted to write a method to be used by several different  
> actions, where would that method be placed?

What is the method related to?

1. If it is related to the dispatching/redirection of the actions  
within a specific controller (rights, authentication, etc), you could  
use a private method on that controller class.
2. If it is related to the view, how a link should show up/or not, or  
how a section of the view will be rendered, then you use the helper  
class for that controller.
3. If it has to do with the business logic surrounding the model  
related to the action, then add the method to the model class.

My 0.02 anyway.
