Can we have have protected methods in our REST controllers?

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

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.

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?

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.

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?

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”?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs