字段覆盖

If you want to customize the presentation of a column, you can define a specially named method in your helper file. The format is #{column_name}_column. So, for example, to customize the :username column displayed on your UsersController, you would add a :username_column method to your UsersHelper file.

如果想定制一个字段的显示方式,你可以在帮助文件中定义一个特别命名的方法。格式是 #{column_name}_column. 所以,举例来说,为了定制使字段 :usernameUsersController中显示,需要在 UsersHelper 文件中添加一个 :username_column 方法。

This override method accepts one argument: the entire record object. It is your responsibility to retrieve the interesting value from the record and to clean the value with h().

这个覆盖方法接受一个参数(argument):整个记录对象。你的职责是,从记录中找回有趣的值并且用 h()清洁这个值。

This override method is used by List and Show.

这个覆盖方法用于列表和显示。

示例:

class User < ActiveRecord::Base
  has_many :roles
end    

module UserHelper
  # joins the first three roles with a hyphen instead of the normal comma.
  # ok, so this one isn't very original.
  def roles_column(record)
    record.roles.first(3).collect{|role| h(role)}.join(' - ')
  end    

  # creates a popup link to the associated division (belongs_to association)
  def division_column(record)
    link_to(h(record.division.name), :action => :show, :controller => 'divisions', :id => record.division.id)
  end
end