Ruby-on-rails 模块的 Rails 未定义方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27117699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails undefined method for Module
提问by gprasant
In Rails, how do you use a specific method from a module. For eg,
在 Rails 中,如何使用模块中的特定方法。例如,
# ./app/controllers/my_controller.rb
class MyController < ApplicationController
include MyModule
def action
MyModule.a_method
end
private
def a_method
...
end
end
# ------------------------------------------------ #
# ./app/helpers/my_module.rb
module MyModule
def a_method
...
end
end
MyControllerincludes MyModule. And in action,I want to use MyModule.a_method(Please note I also have a private a_methodin MyController and I don'twant to use this.)
MyController包括我的模块。而在action,我想用MyModule.a_method(请注意,我也有一个私人a_method的和myController的我并不想用这个。)
Things I've tried :
我尝试过的事情:
1) Defining the method in the module as self.
1)将模块中的方法定义为 self.
def self.a_method
end
2) Using the ::notation in controller (MyModule::a_method)
2)::在控制器 ( MyModule::a_method) 中使用符号
The error that I keep getting is
Undefined method:a_method for MyModule:moduleFor now, I've resorted to using a different name for the modules method. But I'd like to know how to namespace the function with either the Module::or Module.notation
我不断收到的错误是
Undefined method:a_method for MyModule:module现在,我对模块方法使用了不同的名称。但我想知道如何使用Module::orModule.符号命名函数
[UPDATE - 11/24/2014]adding file structure in code, since Rails heavily relies on convention.
[更新 - 2014 年 11 月 24 日]在代码中添加文件结构,因为 Rails 严重依赖约定。
回答by jgraft
So I am not really sure what you are trying to accomplish with your module but a quick solution to get it working is below.
所以我不太确定你想用你的模块完成什么,但下面是一个让它工作的快速解决方案。
- Move my_module.rb out of helpers and into lib/my_module.rb. The helpers directory is for methods that you use in your views. The convention is to utilize helpers that are namespaced after their respective controller or the application_helper.rb for global methods for your views. Not sure if that's what you are trying to accomplish with your module but wanted to throw that out there.
- Create an initializer (you can all it whatever) in config/initializers/custom_modules.rb and add
require 'my_module' - Update the a_method back to be
self.a_method - You can now call
MyModule.a_methodin your app
- 将 my_module.rb 移出 helpers 并移入 lib/my_module.rb。helpers 目录用于您在视图中使用的方法。约定是使用命名空间位于其各自控制器或 application_helper.rb 之后的助手作为视图的全局方法。不确定这是否是您尝试使用模块完成的任务,但想将其扔掉。
- 在 config/initializers/custom_modules.rb 中创建一个初始化程序(你可以随意)并添加
require 'my_module' - 将 a_method 更新为
self.a_method - 您现在可以
MyModule.a_method在您的应用中调用
Don't forget to restart your server for changes to lib/my_module.rb to take effect.
不要忘记重新启动服务器以使对 lib/my_module.rb 的更改生效。
Also, a lot of people reference this postby Yehuda Katz as guidance on where to store code for your app. Thought it might be a helpful reference.
此外,很多人参考Yehuda Katz 的这篇文章作为关于在何处存储应用程序代码的指导。认为这可能是一个有用的参考。
回答by nicolas
if you include MyModule into MyController, all the "instance methods" of the first will be mixed-in into the 2nd.
如果将 MyModule 包含到 MyController 中,则第一个的所有“实例方法”都将混入到第二个中。
So if you only want to call MyModule.a_method, no needto include your module.
因此,如果您只想调用 MyModule.a_method,则无需包含您的模块。
Then you'd want to require (or better autoload) your module before using it. To do so place it in controllers/concerns/my_module.rb, rails (4 at least) should autoload it, otherwise require its file in an intializer
然后你会想要在使用它之前要求(或更好地自动加载)你的模块。为此,将其放置在控制器/问题/my_module.rb 中,rails(至少 4 个)应自动加载它,否则需要将其文件放在初始化器中
# my_module.rb
module MyModule
def self.a_method
...
end
end
should work, but doing
应该工作,但做
# my_module.rb
module MyModule
extend self
def a_method
...
end
end
is more clean to me. You'd like to have a look to rails active support concernto understand the "rails way" on this topic.
对我来说更干净。您想查看 rails主动支持问题以了解有关此主题的“rails 方式”。

