代码之家  ›  专栏  ›  技术社区  ›  Dex

从ActionController调用Alias_方法,NoMethodError

  •  0
  • Dex  · 技术社区  · 14 年前

    在3号轨道,我想打电话 alias_method 从我 before_filter 但我得到了这个错误: NoMethodError in MyController#index

    class ApplicationController < ActionController::Base
    
      before_filter :my_test
    
      protect_from_forgery
      helper :all
    
      def my_test
          debugger
          alias_method :new_method_name, :old_method_name
      end
    end
    

    从调试器:

    (rdb:3) p self.methods.grep /old_method_name/
    ["old_method_name"]
    
    1 回复  |  直到 14 年前
        1
  •  -1
  •   Dex    14 年前

    class ApplicationController < ActionController::Base
    
      before_filter :my_test
    
      protect_from_forgery
      helper :all
    
      def my_test
          self.class_eval("alias_method :new_method_name, :old_method_name")
      end
    end