0
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :null_session

  rescue_from ActiveRecord::RecordNotFound, :with => record_not_found #spazzing out

  def record_not_found
    flash[:error] = 'Could not find specified role'
    redirect_to record_not_found_path
    true
  end

end

How is that wrong? When I try and run a spec i get:

in `<class:ApplicationController>': undefined local variable or method `record_not_found' for ApplicationController:Class (NameError)

Am I missing something Oo

TheWebs
  • 12,470
  • 30
  • 107
  • 211

2 Answers2

2

In the :with => record_not_found argument to rescue_from, record_not_found has not been defined yet, so it's raising the error. You should be providing a symbol instead, as in:

rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

in keeping with the example in http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html#method-i-rescue_from

Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
-1

First the parameter accepted in rescue_from :with => has to be a string or a symbol

Second you should protect a called method with protected to prevent a possible miss usage

class ApplicationController < ActionController::Base

  protect_from_forgery with: :null_session
  # parameter for :with has to be a string or symbol 
  rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

  # to prevent an external access
  protected

  def record_not_found
    flash[:error] = 'Could not find specified role'
    redirect_to record_not_found_path
    true
  end
end
devanand
  • 5,116
  • 2
  • 20
  • 19