views:

126

answers:

1

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.

A: 

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
PreciousBodilyFluids
But I don't want to do this in my controller. I can do whatever, but I need to change an attribute of a particular object (the one in question). The only way I know how to do that is when you're inside a certain model.Basically I just want to always set a boolean in a model depending on what action it just went through.
Stacia