Ruby-on-rails 尝试从 Rails 控制器调用辅助方法时出现 NoMethodError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/453762/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:54:53  来源:igfitidea点击:

NoMethodError when trying to invoke helper method from Rails controller

ruby-on-railsruby

提问by John Topley

I'm getting a NoMethodErrorwhen trying to access a method defined in one of my helper modules from one of my controller classes. My Rails application uses the helperclass method with the :allsymbol as shown below:

NoMethodError当我尝试从我的一个控制器类访问我的一个助手模块中定义的方法时,我得到了一个。我的 Rails 应用程序使用helper带有:all符号的类方法,如下所示:

class ApplicationController < ActionController::Base
  helper :all
  .
  .
end

My understanding is that this should make all of my controller classes automatically include all of the helper modules within the app/helpers directory, therefore mixing in all of the methods into the controllers. Is this correct?

我的理解是,这应该使我的所有控制器类自动包含 app/helpers 目录中的所有辅助模块,因此将所有方法混合到控制器中。这样对吗?

If I explicitly includethe helper module within the controller then everything works correctly.

如果我include在控制器中明确使用辅助模块,则一切正常。

采纳答案by Honza

helper :allmakes all the helpers (yes, all of them) available in the views, it does not include them into the controller.

helper :all使所有帮助程序(是的,所有帮助程序)在视图中可用,它不会将它们包含在控制器中。

If you wish to share some code between helper and controller, which is not very desirable because helper is UI code and controller is, well, controller code, you can either include the helper in the controller, or create a separate module and include that in the controller and the helper as well.

如果您希望在助手和控制器之间共享一些代码,这不是很可取,因为助手是 UI 代码而控制器是控制器代码,您可以将助手包含在控制器中,或者创建一个单独的模块并将其包含在控制器和助手也是如此。

回答by gamecreature

To use the helper methods already included in the template engine:

要使用模板引擎中已经包含的辅助方法:

  • Rails 2: use the @templatevariable.
  • Rails 3: has the nice controller method view_context
  • Rails 2:使用@template变量。
  • Rails 3:有很好的控制器方法 view_context

Example usage of calling 'number_to_currency' in a controller method:

在控制器方法中调用“number_to_currency”的示例用法:

# rails 3 sample
def controller_action
  @price = view_context.number_to_currency( 42.0 ) 
end

# rails 2 sample
def controller_action
  @price = @template.number_to_currency( 42.0 ) 
end

回答by ben

if you need to share a method between a controller and helper/view, you can just define via 'helper_method' in the top of the controller:

如果您需要在控制器和助手/视图之间共享一个方法,您可以通过控制器顶部的“helper_method”进行定义:

class ApplicationController < ActionController::Base
  helper_method :my_shared_method
  ...

  def my_shared_method
    #do stuff
  end
end

hope that helps

希望有帮助

回答by Adrian Dunston

Helper Methods from Controllers

来自控制器的辅助方法

One way to get at your helper methods is simply to include your helper file.

获取辅助方法的一种方法是简单地包含辅助文件。

include LoginHelper
cool_login_helper_method(x,y,z)

This brings all the methods from that helper module into scope in your controller. That's not always a good thing. To keep the scope separate, create an object, imbue it with the powers of that helper, and use it to call the methods:

这会将来自该帮助器模块的所有方法带入您的控制器中。这并不总是一件好事。为了保持作用域分离,创建一个对象,赋予它这个助手的能力,并用它来调用方法:

login_helper = Object.new.extend(LoginHelper)
login_helper.cool_login_helper_method(x,y,z)


Helper :all

帮手:全部

helper :allmakes all of your helper methods from all of your helper modules available to all of your views, but it does nothing for your controllers. This is because helper methods are designed for use in views and generally shouldn't be accessed from controllers. In newer versions of Rails, this option is always on for every controller by default.

helper :all使所有辅助模块中的所有辅助方法可用于所有视图,但它对您的控制器没有任何作用。这是因为辅助方法是为在视图中使用而设计的,通常不应从控制器访问。 在较新版本的 Rails 中,默认情况下每个控制器都启用此选项。

回答by B Seven

For Rails 3, use the view_contextmethod in your controller:

对于 Rails 3,使用view_context控制器中的方法:

def foo
  view_context.helper_method
  ...

Here's an example: http://www.christopherirish.com/2011/10/13/no-view_context-in-rails-3-1-changes/

这是一个例子:http: //www.christopherirish.com/2011/10/13/no-view_context-in-rails-3-1-changes/

回答by Antonio Tapiador

It is probably cleaner to use the helpersmethod:

使用helpers方法可能更干净:

class FooController < ActionController::Base
  def action
    self.class.helpers.helper_method arg
  end
end

回答by Scott Miller

The time when I find this to be most needed is for writing the flash, or custom error checkers. Its nice to use things like link_to helpers in the flash message under some circumstances. I use the following solution to get ActionView helpers into the controller. Be advised that as was mentioned above, this breaks the MVC separation, so if anyone else has a better idea, let me know!

我发现最需要这样做的时候是编写闪存或自定义错误检查器。在某些情况下,在 flash 消息中使用诸如 link_to 助手之类的东西很好。我使用以下解决方案将 ActionView 助手获取到控制器中。请注意,如上所述,这打破了 MVC 分离,所以如果其他人有更好的主意,请告诉我!

Below ApplicationController add this:

在 ApplicationController 下方添加:

class Something
  include Singleton
  include ActionView::Helpers::UrlHelper
end

and inside the ApplicationController, add

在 ApplicationController 中,添加

def foo
  Something.instance
end

and finally, in the controller where you want to access the helper code:

最后,在您要访问帮助程序代码的控制器中:

messages << "<li class='error'>Your have an Error!<%= foo.link_to('Fix This', some_path) %></li>"

Hope that helps in some way!

希望在某种程度上有所帮助!

回答by BitOfUniverse

Any helper can be accessed using @template variable in the controller.

可以使用控制器中的 @template 变量访问任何帮助程序。

@template.my_super_helper

@template.my_super_helper

回答by Nitin

Controller can't access helper methods automatically. We must include them in app controller.

控制器无法自动访问辅助方法。我们必须将它们包含在应用程序控制器中。

module ApplicationHelper

模块应用助手

 def hello_message
    "Hello World"
 end

end

结尾

class ApplicationController < ActionController::Base

类 ApplicationController < ActionController::Base

  include ApplicationHelper

  def message
     hello_message
  end

end

结尾

回答by Milan Novota

Helpers are to be used with templates, ie. views, not in controllers. That's why you can't access the method. If you'd like to share a method between two controllers, you'd have to define it in ApplicationController, for instance. helper :all says that any method you define in any helper file in app/helpers directory will be available to any template.

助手将与模板一起使用,即。视图,而不是在控制器中。这就是您无法访问该方法的原因。例如,如果您想在两个控制器之间共享一个方法,则必须在 ApplicationController 中定义它。helper :all 表示您在 app/helpers 目录中的任何帮助文件中定义的任何方法都可用于任何模板。