Rails NameError

If you’re getting NameError (uninitialized constant UnknownAction) using UnknownAction in the rescue_action_in_public method, try
to use the full reference (::ActionController::UnknownAction) instead of ActionController::UnknownAction. For example :

class ApplicationController < ActionController::Base
 
  attr_accessor :auth_err
 
  #--------------------------------------------
  # rescue_action_in_public()
  def rescue_action_in_public(exception)
    case exception
      when ActiveRecord::RecordNotFound
        unauthorised_action RecordNotFoundError
      when ::ActionController::UnknownAction
        unauthorised_action PageNotFoundError
      else
        log_error(exception)
        redirect_to('/500.html')
      end
  end
 
 
	
  #--------------------------------------------
  # unauthorised_action()
  def unauthorised_action(details=nil)
    # If one unauthorised_action has already occurred, don't need to log the rest
    return false if self.auth_err
		
    # Log the unauthorised_action
    user = "Unknown user"
    user = "User '" + @session[:user].login + "'" if @session[:user]
    msg = user + " denied access to '" + controller_name + "/" + action_name + "'"
    msg += " (" + details + ")" if details
    logger.error(msg) 
    self.auth_err = true
    redirect_to('/404.html') and return false
  rescue
    self.auth_err = true
    return false
  end
 
end



2 Responses to “Rails NameError”  

  1. 1 Jimmy Zimmerman

    Thank you for posting this. This saved me a whole lot of headache! I didn’t realize you could do the :: before the name to better reference the constant.

  2. 2 dave

    No probs Jimmy, glad it helped :)

Leave a Reply