2013-12-20 91 views
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 

這是怎麼回事?當我嘗試和運行規範,我得到:未定義的方法 - record_not_found

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

我這麼想吳

回答

-1

首先在rescue_from接受了參數與示例保持

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

:你應該提供一個符號代替,如結合=>必須是字符串或符號

第二你應該保護一種被保護的方法,以防止可能的遺漏使用

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 
+0

你真的沒有描述什麼是錯的,而你的代碼顯示的解決方案認罪考慮更新以解釋爲什麼這會起作用。 – TheWebs

+0

@TheWebs現在希望這樣做。 – devanand