# Can we have have protected methods in our REST controllers?

**URL:** <https://www.ruby-forum.com/t/can-we-have-have-protected-methods-in-our-rest-controllers/100020>\
**Category:** Rails\
**Created:** [July 26, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/can-we-have-have-protected-methods-in-our-rest-controllers/100020 "2006-07-26T18:57:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [July 26, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/can-we-have-have-protected-methods-in-our-rest-controllers/100020/1 "2006-07-26T18:57:11Z")

</div>

I gotta be quick, sos if it seems rushed.

Latest word from DHH is that the REST conttrollers should only have 4  
methods :

Create, Update, New, Delete

If you don’t know about DHH’s latest thing then leave now, or go read  
about it on his blog.

Does this mean i can’t have private methods in my controller?? I have a  
method that create and new share.

Thanks  
Chris

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [July 26, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/can-we-have-have-protected-methods-in-our-rest-controllers/100020/2 "2006-07-26T19:07:49Z")

</div>

Chris wrote:

> Latest word from DHH is that the REST conttrollers should only have 4  
> methods :
> 
> Create, Update, New, Delete

- New and Edit

> Does this mean i can’t have private methods in my controller?

Nope. I’m guessing your using before\_filter to DRY up some methods which  
is fine.

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [July 26, 2006, 10:04pm UTC](https://www.ruby-forum.com/t/can-we-have-have-protected-methods-in-our-rest-controllers/100020/3 "2006-07-26T22:04:10Z")

</div>

> Nope. I’m guessing your using before\_filter to DRY up some methods which  
> is fine.

how do you use before\_filter to DRY up some methods but not all?

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [July 27, 2006, 1:46am UTC](https://www.ruby-forum.com/t/can-we-have-have-protected-methods-in-our-rest-controllers/100020/4 "2006-07-27T01:46:51Z")

</div>

Chris wrote:

> how do you use before\_filter to DRY up some methods but not all?

Here is how you would DRY this up.

def index  
@products = Product.find(:all)  
end

def new  
@product = Product.new  
end

def create  
@product = Product.create(params[:product])  
end

def show  
@product = Product.find(params[:id])  
end

def edit  
@product = Product.find(params[:id])  
end

def update  
@product = Product.find(params[:id])  
@product.update\_attributes(params[:product])  
end

def destroy  
@product = Product.find(params[:id])  
@product.destroy  
end

Becomes

before\_filter :find\_product, :only =\> %w(show edit update destroy)

def index  
@products = Product.find(:all)  
end

def new  
@product = Product.new  
end

def create  
end

def show  
end

def edit  
end

def update  
@product.update\_attributes(params[:product])  
end

def destroy  
@product.destroy  
end

protected  
def find\_product  
@product = Product.find(params[:id])  
end

When all your controllers are just CRUD, you notice that there are tons  
of things you can extract.

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [July 27, 2006, 11:23am UTC](https://www.ruby-forum.com/t/can-we-have-have-protected-methods-in-our-rest-controllers/100020/5 "2006-07-27T11:23:17Z")

</div>

> When all your controllers are just CRUD, you notice that there are tons  
> of things you can extract.

As i understand it with CRUD you will have more controllers, and  
controllers will frequently call each other.

Lets say you have controller A and B… if B calls A’s action, then  
normally A would want to render its own template, but in this instance i  
dont want to do this, i want to return to whatever B wants to do. HOw  
do you get around this?

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [July 27, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/can-we-have-have-protected-methods-in-our-rest-controllers/100020/6 "2006-07-27T16:46:17Z")

</div>

Chris wrote:

> Lets say you have controller A and B… if B calls A’s action, then  
> normally A would want to render its own template, but in this instance i  
> dont want to do this, i want to return to whatever B wants to do. HOw  
> do you get around this?

Not really sure what your talking about?

render :action =\> “B”?
