Ruby on rails RubyonRails-在html.erb中使用帮助程序

Ruby on rails RubyonRails-在html.erb中使用帮助程序,ruby-on-rails,ruby,erb,helper,Ruby On Rails,Ruby,Erb,Helper,我可能错过了一些东西。 例如,假设我在app/helpers/foo_controller.rb中有一个helper函数,代码如下: def sample_helper(count) #implementaton... end <%= sample_helper(user.id) %> 我想在rails生成的网页中使用这个助手,代码如下: def sample_helper(count) #implementaton... end <%=

我可能错过了一些东西。 例如,假设我在app/helpers/foo_controller.rb中有一个helper函数,代码如下:

def sample_helper(count)
    #implementaton...
end
    <%= sample_helper(user.id) %>
我想在rails生成的网页中使用这个助手,代码如下:

def sample_helper(count)
    #implementaton...
end
    <%= sample_helper(user.id) %>

如果我尝试运行该网页,它将抛出一个错误,说明该方法未定义。
提前谢谢

您的助手是否应该位于名为app/helpers/foo_helper.rb的文件中,该文件包含与助手同名的模块(camellized),如:


这就是Rail自动加载帮助程序的方式。

您没有正确的命名约定

将助手文件命名为app/helpers/foo_helper.rb,其中应包含以下内容:

module FooHelper
  def sample_helper(count)
    "#{count} items" # or whatever
  end
end
现在,从
FooController
呈现的任何视图中,您应该能够使用
sample\u helper
方法


另外,您应该知道,如果您使用rails生成器,这个结构是为您设置的。您所需要做的就是将方法添加到生成的文件中。这样,您就不需要猜测命名约定

例如,此命令将使控制器文件、控制器测试文件、辅助文件和索引视图文件都可以进行自定义

rails g controller foo index

你最好看看铁路工人和他自己,找到一个解决办法