让我们想象一下我有一个类退出Ruby on Rails的控制方法从助手
class Test < ActiveRecord::Base
include AuthenticatorHelper
def test
authenticate_or_fail!
puts "If I fail, this should be unreachable"
end
end
和
module AuthenticationHelper
def authenticate_or_fail!
@user = User.find(params[:token])
unless @user
render :json => {code: 401, :err => 'Unauthorized'} and return
end
end
end
我想要做的是要么认证或用JSON味精回复。但是,它显然忽略我return语句由于嵌套,它总是会打印我的消息
如果我失败了,这应该是不可达
'的ActiveRecord :: Base'没有一个'before_action' – Stefan
这是正确的@Stefan,这就是为什么我建议从'ApplicationController'继承,而不是。我被“ActiveRecord :: Base”追踪了,但是在显示的上下文中使用它肯定是一个错误,所以我只是假设它必须按照描述来修复。 – ulferts