使用rails 4.2.1
我想编写一个强制调用它的控制器来渲染和返回的函数。这可能吗?
def error!
# do stuff to define error
# now render error
render
end
def SomeController < ActionController::Metal
include AbstractController::Rendering
include ActionController::Rendering
include AbstractController::Callbacks
include ActionController::Renderers::All
include ActionController::ImplicitRender
def some_action
if something
# good - continue
else
# bad - error!
error!
# how to force controller to stop execution and return here?
# return error! # I could do this
# but could I do it in the error! function?
end
puts 'Still Running'
end
end
答案 0 :(得分:1)
检查
def some_action
if something
# good - continue
else
return error!
end
puts 'Still Running'
end
另一种选择是在error!
内引发异常,并在ApplicationController
级别设置一些rescue_from阻止