Forum: Ruby on Rails Before_filter get name of controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nick S. (Guest)
on 2005-12-12 23:09
Hi,

I have written a before_filter to check if a person is authorized to
access parts of a controller like so:

  def check_authorized_ads
    @rights = Right.find_by_user_id(1)
    unless @rights.ads
      flash[:notice] = "You are not authorized."
      redirect_to(:controller => "user", :action => "login")
  end

In my rights table I have a column for each controller, indicating if a
user is authorized or not. The easiest way is to write a new
before_filter for each controller, like:

  def check_authorized_controllerx
    @rights = Right.find_by_user_id(1)
    unless @rights.controllerx
      flash[:notice] = "You are not authorized."
      redirect_to(:controller => "user", :action => "login")
  end

But that is not according to the DRY principles. Is their a way to pass
the name of the controller or another param with the before_filter?
Thanks.

Kind regards,

Nick
This topic is locked and can not be replied to.