我可能缺少一些东西。 比方说我有一个辅助功能,在app /佣工/ foo_controller.rb,代码如下:Ruby on Rails - 在html.erb中使用助手
def sample_helper(count)
#implementaton...
end
,我想用这个帮手通过轨道生成一个网页,该代码如下:
<%= sample_helper(user.id) %>
如果我尝试运行网页,它会抛出一个错误,指出该方法未定义。 在此先感谢!
我可能缺少一些东西。 比方说我有一个辅助功能,在app /佣工/ foo_controller.rb,代码如下:Ruby on Rails - 在html.erb中使用助手
def sample_helper(count)
#implementaton...
end
,我想用这个帮手通过轨道生成一个网页,该代码如下:
<%= sample_helper(user.id) %>
如果我尝试运行网页,它会抛出一个错误,指出该方法未定义。 在此先感谢!
是你的帮助应该是在一个名为app /佣工/ foo_helper.rb包含同名AA模块作为助手(骆驼化)文件一样:
module FooHelper
def sample_helper(cont)
# implementation
end
end
就是这样铁路汽车加载助手。
你不完全有正确的命名约定。
名称你的助手文件app/helpers/foo_helper.rb
和它,你应该有这样的:
module FooHelper
def sample_helper(count)
"#{count} items" # or whatever
end
end
而现在,从FooController
呈现的任何观点,你应该能够使用sample_helper
方法。
另外,你应该知道,如果你使用rails generator这个结构是为你设置的。您只需将方法添加到生成的文件中即可。这样你就不需要猜测命名约定。
例如,该命令将生成控制器文件,控制器测试文件,帮助程序文件和索引视图文件,所有这些都可供您自定义。
rails g controller foo index
它可能会更好地为您寻找railscasts和他自己找到一个解决方案