Skip to content
scambra edited this page May 12, 2012 · 22 revisions

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.

Helper Override

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 or #{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. Later name will override columns from all models (unless you use clear_helpers method in ApplicationController) or if you put it in ApplicationHelper.

In v2.3 and previous versions format was only #{column_name}_form_column.

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

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

Until v2.3 the second argument was only the input name attribute 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

Since version 2.3, the second argument (options hash) contains the HTML id, class and name attributes generated by ActiveScaffold for the form element. You will need to use these if you generate the HTML directly rather than using Rails form helpers. For example, options could be

{:name=>"record[member][1304426504155][work_location]", :class=>"work_location-input",
 :id=>"record_work_location_1206_member_1304426504155"}

So if you wanted to use your own <select> tag, you could write

my_select = "<select id='#{options[:id]}', name='#{options[:name]}', class='#{options[:class]}'>" +
            ... your selection options ... +
            "</select>"

Partial Override (overriding the form element and the label)

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:

app/views/roles/_description_form_column.html.erb

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

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].

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>
Clone this wiki locally