尝试从 Rails 控制器调用辅助方法时出现 NoMethodError [英] NoMethodError when trying to invoke helper method from Rails controller

查看:22
本文介绍了尝试从 Rails 控制器调用辅助方法时出现 NoMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm getting a NoMethodError when trying to access a method defined in one of my helper modules from one of my controller classes. My Rails application uses the helper class method with the :all symbol as shown below:

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

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

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?

如果我明确地include 控制器中的辅助模块,则一切正常.

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

推荐答案

helper :all 使所有的助手(是的,所有的)在视图中可用,它不包括它们到控制器.

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

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

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.

这篇关于尝试从 Rails 控制器调用辅助方法时出现 NoMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆