Question

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

Was it helpful?

Solution

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top