# "Common action" for a controller

**URL:** <https://www.ruby-forum.com/t/common-action-for-a-controller/114166>\
**Category:** Rails\
**Created:** [September 9, 2007, 12:37am UTC](https://www.ruby-forum.com/t/common-action-for-a-controller/114166 "2007-09-09T00:37:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![twp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@twp](https://www.ruby-forum.com/u/twp)\
**Post date:** [September 9, 2007, 12:37am UTC](https://www.ruby-forum.com/t/common-action-for-a-controller/114166/1 "2007-09-09T00:37:31Z")

</div>

Sorry if this has been asked before; it seems like a simple problem.

I have a controller called ItemController which displays information  
about specific items to a user on various pages. The routing looks like  
“items/:item/:action”. Is there any way to automatically look up the  
:item in the items table (it’s just an ID).

I tried:

before\_filter { @current\_item = Item.find(:first, @params[:item],  
:include =\> [:set, :card]) }

but apperently filters don’t have access to params. Is there any other  
simple way? Or is this somehow bad practice?

---

<div class="post-metadata">

**Author:** ![twp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@twp](https://www.ruby-forum.com/u/twp)\
**Post date:** [September 9, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/common-action-for-a-controller/114166/2 "2007-09-09T12:06:47Z")

</div>

Tom Potter wrote:

> :include =\> [:set, :card]) }
> 
> but apperently filters don’t have access to params. Is there any other  
> simple way? Or is this somehow bad practice?

From the API docs:

class WeblogController \< ActionController::Base  
before\_filter { |controller| false if  
controller.params[“stop\_action”] }  
end

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![twp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@twp](https://www.ruby-forum.com/u/twp)\
**Post date:** [September 9, 2007, 6:21pm UTC](https://www.ruby-forum.com/t/common-action-for-a-controller/114166/3 "2007-09-09T18:21:13Z")

</div>

Tom Potter wrote:

> Sorry if this has been asked before; it seems like a simple problem.
> 
> I have a controller called ItemController which displays information  
> about specific items to a user on various pages. The routing looks like  
> “items/:item/:action”. Is there any way to automatically look up the  
> :item in the items table (it’s just an ID).
> 
> I tried:
> 
> before\_filter { @current\_item = Item.find(:first, @params[:item],  
> :include =\> [:set, :card]) }
> 
> but apperently filters don’t have access to params. Is there any other  
> simple way? Or is this somehow bad practice?

alternatively

before\_filter :get\_current\_item  
def get\_current\_item  
@current\_item = Item.find(:first, _blah_)  
end
