-
Notifications
You must be signed in to change notification settings - Fork 330
Form Overrides
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).
These overrides can be used to hide fields on the form, or even to replace standard inputs with javascript-enabled inputs.
These overrides are currently used by Create and Update.
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.
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.
Example:
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
- 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
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.
Example:
- in app/views/roles/_description_form_column.rhtml
- this works too:
Sometimes you may wish to access the active scaffold configuration in your partial override. By default, the variable ApiColumn). 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]
.
- in app/views/roles/city_form_column.rhtml
<% scope ||= nil >
<= column.label >,
<= active_scaffold_config.columns[:state].label >
<= active_scaffold_config.columns[:zip].label >
<
<= form_column(active_scaffold_config.columns[:state], scope) >
<= form_column(active_scaffoldconfig.columns[:zip], scope) %>
-