表单覆盖

If you want to customize the form interface for a column, you have two choices. You can define a specially named partial, or you can define a specially named method in your helper file. The difference between the partial and the helper method is that the partial will be responsible for displaying the label and everything, whereas the helper will only be responsible for displaying the input element (or other interface).

如果你想为一个字段定制表单界面,你有两个选择。你可以定义一个特别命名的局部(partial), 或者你也可以在你的帮助文件中定义一个特别命名的方法。局部(Partial)和帮助方法之间的不同点在于,局部(partial)负责显示标签以及每件事物,而帮助方法只负责显示输入元素(或者其他界面)。

These overrides can be used to hide fields on the form, or even to replace standard inputs with javascript-enabled inputs.

这些覆盖可以用来在表单中隐藏字段,甚至可以将标准输入替换为以javascript激活的输入。

These overrides are currently used by Create and Update.

这些覆盖普遍用于创建和修改。

Helper Override(帮助覆盖)

The helper override is only responsible for display the input element (or whatever else you want). It should be named #{column_name}_form_column. If you want the post to be handled by ActiveScaffold, you need to use the params[:record] namespace. With the helper override this is taken care of if you use the second argument: the input name. See the example below for more details.

帮助覆盖只负责显示输入元素(或者无论其他什么你想要的)。它应该被命名为 #{column_name}_form_column。如果你想要日志被ActiveScaffold处理,你需要用params[:record]命名空间。如果你使用第二个argument: the inputname的话,在帮助覆盖中你更加需要注意这点。从下面例子中您可以得到更多细节。

Note that with even with subforms, helper overrides only apply to the current controller.

注意,即使有子表单,帮助覆盖也只作用到当前控制器。

Warning: We were able to patch date_select, time_select, and datetime_select to support a :name parameter. With the other set of methods (select_date, select_time, select_datetime) you must use the :prefix parameter instead. See the example below.

警告:我们有能力修补date_select, time_select 和 datetime_select 来支持:name参数。有了其他方法集(select_date, select_time, select_datetime), 你必须使用:prefix参数作为替换。可以看下面的例子。

示例:

module UserHelper
  # display the "is_admin" field as a checkbox instead of a dropdown
   def is_admin_form_column(record, input_name)
      check_box :record, :is_admin, :name => input_name
   end     

   def date_received_form_column(record, input_name)
      # with date_select we can use :name
      date_select :record, :date_received, :name => input_name
      # but if we used select_date we would have to use :prefix
      #select_date record[:date_received], :prefix => input_name
    end
end    

Partial Override(局部覆盖,覆盖表单元素和标签)

The partial override is responsible for displaying the field label, element, description, etc.. It should be named _#{column_name}_form_column.rhtml and be placed in your controller’s views folder.

局部(partial)覆盖负责显示字段标签、元素、描述等等。它应该被命名为 _#{column_name}_form_column.rhtml 并且放置在你的控制器视图文件夹下。

示例:

# in app/views/roles/_description_form_column.rhtml 

<label>Description</label>
<%= text_area :record, :description, :cols => 25, :rows => 10 %> 

# this works too: 

<label>Description</label>
<%= text_area_tag('record[description]', @record.description,
:size => '25x10')

Sometimes you may wish to access the active scaffold configuration in your partial override.

By default, the variable “column” will be passed into your partial. This is not the column for your model, as it easily could be confused to be. Rather, this is the configuration column (see API: Column). The column variable has 3 important attributes: column.name, column.label, and column.description. You can access other configuration columns by using active_scaffold_config.columns [:column_name].

有时,你可能希望在你的局部重写中访问Active Scaffold的配置。默认的,变量“字段(column)”将被传递进你的局部(partial)。这不是对应你模型的字段,虽然它很容易被误解为是。更正确的说,这是配置的字段(见API:字段)。这个字段变量有3个很重要的属性: column.name, column.label, 和 column.description.你可以通过使用 active_scaffold_config.columns [:column_name]访问其他的配置字段。

# in app/views/roles/_city_form_column.rhtml
<% scope ||= nil %>
<dl>
  <dt>
    <label for="<%= "record_#{column.name}" %>">
      <%= column.label %>,
      <%= active_scaffold_config.columns[:state].label %>
      <%= active_scaffold_config.columns[:zip].label %>
    </label>
  </dt>
  <dd>
    <%= form_column column, scope %>,
    <%= form_column(active_scaffold_config.columns[:state], scope) %>
    <%= form_column(active_scaffold_config.columns[:zip], scope) %>
  </dd>
</dl>