For automatic building of forms i did something like this:
<% @columns.each do |column, title| -%>
<%= title -%>:
<%= auto_form_method f, @resource, column, auto_rest_data -%>
<% end -%>
and @columns should be populated like this in the controller:
@columns= [[:attribute1, ‘title1’],
[:attribute2, ‘title2’]] # …etc…
@foreign_keys= { :other_model_id => OtherModel }
auto_form_method is a method helper:
# Standard selection of field type given the column type.
def auto_form_method form, resource, attribute, auto_rest_data
# Check if attribute is indeed a model relation foreign key
if @foreign_keys.include? attribute
model= @foreign_keys[attribute]
auto_model_foreign_key_select form, resource, model, attribute
else
auto_standard_form_method form, resource, attribute
end
end
# Creation of a select tag given a foreign key
def auto_model_foreign_key_select form, resource, model, attribute
option_tags= options_from_collection_for_select model.find(:all),
'id',
‘label’,
h(resource.send(attribute))
select_tag "resource[#{attribute}]", option_tags, options
end
# This Helper returns the apropiate form method
# for the resource and attribute
# TODO: This is pretty ugly :) ...
# Is there any other way to acomplish this functionality?
def auto_standard_form_method form, resource, attribute
case resource.column_for_attribute(attribute).type
when :string : form.text_field(attribute)
when :text : form.text_area(attribute)
when :integer : form.text_field(attribute)
when :float : form.text_field(attribute)
when :decimal : form.text_field(attribute)
when :datetime : form.text_field(attribute)
when :timestamp : form.text_field(attribute)
when :time : form.text_field(attribute)
when :date : form.text_field(attribute)
when :binary : form.check_box(attribute)
when :boolean : form.check_box(attribute)
end
end
Summary: It is not automatic nor easy to acomplish, IMO!!!
EmmanuelOga.blogspot.com