在rails控制器中渲染后强制返回?

时间:2015-05-15 07:40:12

标签: ruby-on-rails ruby ruby-on-rails-4

使用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

1 个答案:

答案 0 :(得分:1)

检查

def some_action
   if something
     # good - continue
   else         
     return error!         
   end
  puts 'Still Running'
end

另一种选择是在error!内引发异常,并在ApplicationController级别设置一些rescue_from阻止