红宝石。从重写方法调用Super

时间:2012-02-27 16:25:40

标签: ruby-on-rails ruby oop inheritance

我试图覆盖redirect_to方法,为get请求添加额外的param(如果存在)

redirect_to方法在这里

module ActionController
...................

  module Redirecting
    extend ActiveSupport::Concern
    include AbstractController::Logger
    ...........................

    def redirect_to(options = {}, response_status = {}) #:doc:
      ............................
      self.status        = _extract_redirect_to_status(options, response_status)
      self.location      = _compute_redirect_to_location(options)
      self.response_body = "<html><body>You are being <a href=\"#    {ERB::Util.h(location)}\">redirected</a>.</body></html>"
    end

  end

end

以下是我试图覆盖的方式

module ActionController

    module Redirecting
      def redirect_to(options = {}, response_status = {})

        if options
          if options.is_a?(Hash)
            options["custom_param"] = @custom_param
          else
            if options.include? "?" 
              options = options + "&custom_param=true"
            else
              options = options + "?custom_param=true"
            end 
          end
        end 

        super 

      end

  end

end 

我显然做错了,超级方法调用无法按照我想要的方式工作。希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:4)

我认为这里的问题是你正在重新定义redirect_to方法,而不是在新的地方定义。 super无法调用原始内容,因为它已不存在。

您要查找的方法是alias_method_chain

module ActionController
  module Redirecting

    alias_method_chain :redirect_to, :extra_option

    def redirect_to_with_extra_option(options = {}, response_status = {})

      if options
        ...
      end

      redirect_to_without_extra_option(options, response_status)
    end
  end
end

尽管如此,我认为Rails更友好的方式是覆盖redirect_to

中的ApplicationController
class ApplicationController
  ....
  protected
    def redirect_to(...)
      if options
        ....
      end
      super
    end
end

这种方法的好处在于您没有修补rails,现在应用程序控制器中已经设置了特定于应用程序的参数。

相关问题