Sexy_Forms_AR.rb Revision 95149780e2e1 (Tue Jun 01 2010 at 21:01) - Diff Link to this snippet: https://friendpaste.com/mfvJvID3BEZfqngTyV5Lh Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 12345678910111213141516171819202122232425262712829303132333435363738394041module SexyForms def field(control, attribute, options = {}) field_class = options.delete(:field_class) note = options.delete(:note) required = options.delete(:required) errors_for = options.delete(:errors_for) || attribute options[:label] = Inflector.humanize(attribute) if options[:label].blank? errors = errors_on_attribute(errors_for) field_classes = ["field"] field_classes << "error" if errors.any? field_classes << options[:field_class] if options[:field_class] field_classes << control.to_s field_classes << "required" if required if options[:name] content = send("#{control}_field", options) else content = send("#{control}_control", attribute, options) end content += %(<p class="note">#{note}</p>) if note content += %(<p class="error">#{errors}</p>) if errors.any? tag :div, content, :class => field_classes.join(' ') end def errors_on_attribute(attribute) # @_obj comes automagically from merb [@_obj.errors[attribute.to_s]].flatten.join(' and ') rescue [] end # Pass more_html for cancel link (or whatever) def form_submit(string, options = {}) %(<div class="field controls">)+ (submit_button string, :class => "positive") + (options[:cancel] ? link_to("Cancel", options[:cancel], :class => "cancel") : '')+ %(</div>) endend