I have a controller with some non standard actions, like admin, moderate, etc. I tried using the hook before_admin
it that didn't work. Is it possible to use these hooks for my custom actions?
Sorry for the lack of clarity. Say I have something that I want to happen before saving..it's easy to just do a before_save :do_whatever
inside the model. I want to have开发者_高级运维 something like a before_admin :do_something
or before_moderate :do_something
, where these are custom methods in my controller.
If I understand you right, you need a before_filter.
class MyController < ActionController::Base
before_filter :do_whatever, :only => :admin
def admin
# Code for your controller action
end
private
def do_whatever
# Code that runs before the admin action
end
end
See here for more information.
Edit: If you want different controllers/actions to do different things to a model, then the appropriate place to put that logic is in the controller. Proper MVC design keeps the model ignorant of the controller/action that's manipulating it. It may seem inconvenient now, but in the long run it'll go a long way towards keeping your code clean and usable.
Edit again: Sorry, you asked how and not why, and I missed it completely. You'd do:
def action
@thing = Thing.find(params[:id])
@thing.boolean = true
@thing.save
end
精彩评论