2

有一个模块:

module ActionDispatch
  module Routing
  end
end

和方法:

def add_movie_path
end

def edit_movie_path
end

我如何添加到模块路由这个方法?

这是唯一的方法吗

4

3 回答 3

4

尝试:

module ActionDispatch
  module Routing
    def add_movie_path
    end

    def edit_movie_path
    end
     module_function :edit_movie_path
  end
end

这样你就可以像这样调用实例方法

class Make
   include ActionDispatch::Routing
end 


class MakeAll
   def only_needs_the_one_method
      ActionDispatch::Routing.edit_movie_path
   end
end 

您还可以通过使用将其定义为类方法self.class_name,然后像这样直接访问它:

module ActionDispatch
  module Routing
    def self.add_movie_path
    end

    def self.edit_movie_path
    end
  end
end

class Make
    include ActionDispatch::Routing
   def do_something
     ActionDispatch::Routing.add_movie_path
   end
end 


class MakeAll
   def only_needs_the_one_method
      ActionDispatch::Routing.edit_movie_path
   end
end

有关更多信息,请参阅Modules Magic

于 2013-03-07T15:14:03.127 回答
2

除非我误解了您的要求,否则类似:

module ActionDispatch
  module Routing
    def add_movie_path
    end

    def edit_movie_path
    end
  end
end

或者,您可以使用module_eval.

于 2013-03-07T15:09:13.947 回答
0

只需将您的方法放在模块中即可。

module ActionDispatch
  module Routing
    def add_movie_path
    end

    def edit_movie_path
    end  
  end
end
于 2013-03-07T15:11:47.527 回答