inject code before or after a restful action

0 views
Skip to first unread message

Brian

unread,
Feb 26, 2008, 2:44:04 PM2/26/08
to resources_controller
One thing I would really like to see in resources_controller is
something similar to what the resource_controller and make_resourceful
plugins do. Right now with resources_controller my understanding is
if I want to do something before or after a restful action I have do
something like this:

class PostsController < ApplicationController
resources_controller_for :posts
def create
self.resource = new_resource
resource.user = current_user
save_resource
end
end

The above is not very DRY across controllers and I am having to access
internal code of the plugin.

Here is how resource_controller handles it:

class PostsController < ApplicationController
resource_controller
create.before {@post.user = current_user}
end

And here is how make_resourceful handles it:

class PostsController < ApplicationController
make_resourceful do
before :create do
@post.user = current_user
end
end
end

Both of the above plugins shield you from the internals and provide an
expressive interface to include the additional code you need to inject
before or after any restful action. This would be really nice to have
in resources_controller. Thoughts?

Best,

Brian

sjansen

unread,
Apr 14, 2008, 3:40:45 AM4/14/08
to resources_controller
I did it this way...

class PostsController < ApplicationController
resources_controller_for :posts
def create
# do something before
super
# do something after
end
end

or like this, if you want it depending on each part...

class PostsController < ApplicationController
resources_controller_for :posts
def create
# do something before - return a boolean
super if "before boolean is true"
# do something after - if "before boolean is true" and/or
"resource_saved?"
end
end

Hope this helps :)

On 26 Feb., 21:44, Brian <btkn...@gmail.com> wrote:
> One thing I would really like to see in resources_controller is
> something similar to what the resource_controller and make_resourceful
> plugins do. Right now with resources_controller my understanding is
> if I want to do something before or after a restful action I have do
> something like this:
>
> class PostsController < ApplicationController
> resources_controller_for :posts
> def create
> self.resource = new_resource
> resource.user = current_user
> save_resource
> end
> end
>
> The above is not very DRY across controllers and I am having to access
> internal code of the plugin.
>
> Here is how resource_controller handles it:
>
> class PostsController < ApplicationController
> resource_controller
> create.before {...@post.user = current_user}
Reply all
Reply to author
Forward
0 new messages