Ruby on rails 3.2 从Ruby on Rails中的嵌套方法重定向

Ruby on rails 3.2 从Ruby on Rails中的嵌套方法重定向,ruby-on-rails-3.2,Ruby On Rails 3.2,这里有一个类似于我的代码。我需要做的是从一个嵌套方法重定向到另一个页面,停止执行它从调用的所有操作。在这种情况下,它是方法2。它应该重定向到'/home/index',我需要防止重定向到'/home/index123' class MyController::ApplicationController def index #some work method1 #some work that must be done if there is no

这里有一个类似于我的代码。我需要做的是从一个嵌套方法重定向到另一个页面,停止执行它从调用的所有操作。在这种情况下,它是方法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

你的想法?

让你的重定向成为一个变量。将此变量设置为方法的返回值。 例如:

 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

我有2个私有方法,这将是不好的,因为我必须在每个方法和索引中检查此变量。不理解您的concernI更新的索引和方法1。看看他们。底线是,我想马上重定向,在这里我会调用它。如果你能像我的解决方案那样只用一种方法,为什么你要用三种方法重复重定向逻辑。您只需在其中一个私有方法中设置重定向,如果为nil,则将索引操作中的to_操作变量设置为默认url。这个解决方案是干净的,避免了代码的重复,使它更易于阅读。您忘记了索引有“如果没有重定向,必须完成一些工作”。所以,如果有办法重定向,这项工作一定不能做。