0

这是一个类似于我的代码。我需要做的是从嵌套方法重定向到其他页面并停止执行它调用的所有操作。在这种情况下,它是方法 2。它应该重定向到'/home/index',我需要防止重定向到'/home/index123'

    class MyController::ApplicationController

    def index
     #some work
      method1
     #some work  that must be done if there is no redirect
     #some work   that must be done if there is no redirect
      redirect_to '/home/index123' 
    end

    private

    def method1
     #some work
     method2
     #some work that must be done if there is no redirect
     #some work  that must be done if there is no redirect
    end

    def method2
     #some work
     #I need to redirect to other page just from here!
     redirect_to '/home/index'
    end


end

你的想法?

4

1 回答 1

1

使您的重定向成为变量。将此变量设置为方法的返回值。例如:

 def method2
   #some work
   #I need to redirect to other page just from here!
   '/home/index'
 end



def index
     #some work
      to_action = method1
      redirect_to to_action
    end
于 2012-08-20T15:55:42.050 回答