Edge Rails 的更新:如何更方便的调用Helper

Posted by yudi
on Sunday, December 16


如果你时常整理项目中的文档,很有可能遇到的情况是:在app/helpers文件夹下堆积了许多helper (不仅仅是创建Controller(控制器)时1对1自动生成的 helper,而是指为处理其他常用命令所定制的helpers)。既然这样,在您应用程序的Controller中也会包涵一些 helper的声明,以使它们被调用:

1
2
3
4
5
6
7
class ApplicationController < ActionController::Base

  # 我们需要让许多helpers提供的功能覆盖整个应用程序
  [:formatting, :highlighting, :i18n].each { |h| helper h }

  ...
end

现在,我们可以更加简便的调用helpers

1
2
3
4
5
6
7
class ApplicationController < ActionController::Base

  # 使所有 app/helpers/**/ 被应用:
  helper :all

  ...
end

作者二月二十六日更新:我曾经认为您必须通过建立在helper下的子文件夹才能正确的调用它们(helper),但是 DHH 后来更正了我。任何出现在helper子文件夹内的helpers都会被吸收到控制器当中。

这项更新已经被预设在Rails2.0 里,这意味着所用存在于 helper目录 下的helper 都会被自动调用。

原文作者是 Ryan Daigle, 请访问他的博客
本片译文的原文地址:http://ryandaigle.com/articles/2007/2/26/what-s-new-in-edge-rails-quick-way-to-include-all-helpers-in-your-controllers

Comments

Leave a response