url_for.rb 915 字节
Newer Older
1 2 3 4 5 6
# Includes #url_for into the host class (e.g. an abstract controller or mailer). The class
# has to provide a RouteSet by implementing the #_routes methods. Otherwise, an exception
# will be raised.
#
# Note that this module is completely decoupled from HTTP - the only requirement is a valid 
# #_routes implementation.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
module AbstractController
  module UrlFor
    extend ActiveSupport::Concern
    include ActionDispatch::Routing::UrlFor

    def _routes
      raise "In order to use #url_for, you must include routing helpers explicitly. " \
            "For instance, `include Rails.application.routes.url_helpers"
    end

    module ClassMethods
      def _routes
        nil
      end

      def action_methods
        @action_methods ||= begin
          if _routes
            super - _routes.named_routes.helper_names
          else
            super
          end
        end
      end
    end
  end
end