-
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 #{class_name}_#{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 options hash. See the example below for more details.
Warning: In v2.3 and previous versions format was #{column_name}_form_column
.
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 UsersHelper # display the "is_admin" field as a checkbox instead of a dropdown def user_is_admin_form_column(record, options) check_box :record, :is_admin, options end def user_date_received_form_column(record, options) # with date_select we can use :name date_select :record, :date_received, options # but if we used select_date we would have to use :prefix #select_date record[:date_received], options.merge(:prefix => options[:name]) end end
Warning: Until v2.3 the second argument was only the input name instead of a full options hash. See example below:
module UsersHelper # display the "is_admin" field as a checkbox instead of a dropdown, in v2.3 there is no class name prefix def is_admin_form_column(record, input_name) check_box :record, :is_admin, :name => input_name end
The partial override is responsible for displaying the field label, element, description, etc.. It should be named _#{column_name}_form_column.html.erb
and be placed in your controller’s views folder.
Example:
# in app/views/roles/_description_form_column.html.erb <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 current column is sent in the variable column (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]
.
# in app/views/roles/_city_form_column.html.erb <% 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>