Ruby on rails 未定义的方法-未找到记录 class ApplicationControllerrecord_not_found#突然弹出 未找到def记录 flash[:error]=“找不到指定的角色” 将\u重定向到记录\u未找到\u路径 真的 结束 结束
怎么会错呢?当我尝试运行规范时,我得到:Ruby on rails 未定义的方法-未找到记录 class ApplicationControllerrecord_not_found#突然弹出 未找到def记录 flash[:error]=“找不到指定的角色” 将\u重定向到记录\u未找到\u路径 真的 结束 结束,ruby-on-rails,rescue,Ruby On Rails,Rescue,怎么会错呢?当我尝试运行规范时,我得到: 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 ActiveRe
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
“'”中的:ApplicationController:Class(NameError)的未定义局部变量或方法“record\u not\u found”
我是否缺少
中的Oo:使用=>record\u not\u found
参数从中营救,record\u not\u found
尚未定义,因此引发了错误。您应该提供一个符号,如:
in `<class:ApplicationController>': undefined local variable or method `record_not_found' for ApplicationController:Class (NameError)
与中的示例一致,首先,rescue_from:with=>中接受的参数必须是字符串或符号
其次,应该使用protected保护被调用的方法,以防止可能的未命中使用
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
class ApplicationController:未找到记录
#防止外部访问
受保护的
未找到def记录
flash[:error]=“找不到指定的角色”
将\u重定向到记录\u未找到\u路径
真的
结束
结束
<>代码>你并没有真正描述什么是错误的,当你的代码显示了解决方案时,请考虑更新以解释为什么这是有效的。
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