views:

70

answers:

1

Say I have the following controller and want to restrict :edit, :update, and :destroy to current_user's own foos.

class FooController < InheritedResources::Base
  before_filter :login_required
  respond_to :html

  def show
    @foo = Foo.find params[:id]
    show!
  end

  protected

  def collection
    @foos ||= Foo.all
  end

  def begin_of_association_chain
    current_user
  end
end

My simple and perhaps naive question is: Can the above be refactored to look better? It feels like I'm overriding too much of inherited_resources.

A: 

I typically went with a before filter along the lines of :lookup_foo. (calling it with :except => [:index,:new,:create]). Then I would have a line like @foo = current_user.foos.find(params[:id]).

A similar method can be used for the index action, and both can be adapted to give admin's more access.

Scott S.