Class Index [+]

Quicksearch

Ramaze::Helper::BlueForm

The BlueForm helper tries to be an even better way to build forms programmatically. By using a simple block you can quickly create all the required elements for your form.

See {Ramaze::Helper::BlueForm::Form} for all the available methods.

## Form Data

As stated earlier it’s possible to pass an object to the form_for() method. What kind of object this is, a database result object or an OpenStruct object doesn’t matter as long as the attributes can be accessed outside of the object (this can be done using attr_readers). This makes it extremely easy to directly pass a result object from your favourite ORM. Example:

    @data = User[1]

    form_for(@data, :method => :post) do |f|
      f.input_text 'Username', :username
    end

If you don’t want to use an object you can simply set the first parameter to nil.

## HTML Output

The form helper uses Gestalt, Ramaze’s custom HTML builder that works somewhat like Erector. The output is very minimalistic, elements such as legends and fieldsets have to be added manually. Each combination of a label and input element will be wrapped in

tags.

When using the form helper as a block in your templates it’s important to remember that the result is returned and not displayed in the browser directly. When using Etanni this would result in something like the following:

    #{form_for(@result, :method => :post) do |f| do
      f.input_text 'Text label', :textname, 'Chunky bacon!'
    end}

@example Creating a basic form

 form_for(@data, :method => :post) do |f|
   f.input_text 'Username', :username
 end

Public Instance Methods

form_error(name, message) click to toggle source

Manually add a new error to the form_errors key in the flash hash. The first parameter is the name of the form field and the second parameter is the custom message.

@param [String] name The name of the form field to which the error

 belongs.

@param [String] message The custom error message to show.

    # File lib/ramaze/helper/blue_form.rb, line 80
80:       def form_error(name, message)
81:         if respond_to?(:flash)
82:           old = flash[:form_errors] || {}
83:           flash[:form_errors] = old.merge(name.to_s => message.to_s)
84:         else
85:           form_errors[name.to_s] = message.to_s
86:         end
87:       end
form_errors() click to toggle source

Returns the hash containing all existing errors and allows other methods to set new errors by using this method as if it were a hash.

@return [Array] All form errors.

     # File lib/ramaze/helper/blue_form.rb, line 95
 95:       def form_errors
 96:         if respond_to?(:flash)
 97:           flash[:form_errors] ||= {}
 98:         else
 99:           @form_errors ||= {}
100:         end
101:       end
form_errors_from_model(obj) click to toggle source

Retrieve all the form errors for the specified model and add them to the flash hash.

@param [Object] obj An object of a model that contains form errors.

     # File lib/ramaze/helper/blue_form.rb, line 109
109:       def form_errors_from_model(obj)
110:         if obj.respond_to?(:errors)
111:           obj.errors.each do |key, value|
112:             if value.respond_to?(:first)
113:               value = value.first
114:             end
115: 
116:             form_error(key.to_s, value % key)
117:           end
118:         end
119:       end
form_for(form_values, options = {}, &block) click to toggle source

The form method generates the basic structure of the form. It should be called using a block and it’s return value should be manually sent to the browser (since it does not echo the value).

@param [Object] form_values Object containing the values for each form

 field.

@param [Hash] options Hash containing any additional form attributes

 such as the method, action, enctype and so on.

@param [Block] block Block containing the elements of the form such as

 password fields, textareas and so on.
    # File lib/ramaze/helper/blue_form.rb, line 65
65:       def form_for(form_values, options = {}, &block)
66:         form = Form.new(form_values, options)
67:         form.build(form_errors, &block)
68:         form
69:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.