Parent

Files

JqmobileHelpers::FormBuilder

JqmobileHelpers Form Helper

Credits to github.com/alexreisner for his Informant formbuilder

Provides a set of helper methods for jquery-mobile form elements

Displays rails helpers fields within a div tag, label on one line, field below it. Simplify your form code by encapsulating all aspects of a field (label, description, etc) in a single method call.

All field methods accept an options hash like the standard Rails FormBuilder methods

Public Instance Methods

date_select(method, options = {}) click to toggle source

Standard Rails date selector.

     # File lib/jqmobile_helpers/form_builder.rb, line 101
101:     def date_select(method, options = {})
102:       options[:include_blank] ||= false
103:       options[:start_year]    ||= 1801
104:       options[:end_year]      ||= Time.now.year
105:       options[:label_for]       = "#{object_name}_#{method}_1i"
106:                   build_shell(method, options) { super }
107:     end
field_set(legend = nil, options = nil, &block) click to toggle source

Render a field set (HTML fieldset tag). Takes the legend (optional), an options hash, and a block in which fields are rendered.

     # File lib/jqmobile_helpers/form_builder.rb, line 183
183:     def field_set(legend = nil, options = nil, &block)
184:       @template.content_tag(:fieldset, options) do
185:         (legend.blank?? "" : @template.content_tag(:legend, legend)) +
186:         @template.capture(&block)
187:       end
188:     end
integer_select(method, options = {}) click to toggle source

Integer select field. Takes options :first, :last, and :step.

     # File lib/jqmobile_helpers/form_builder.rb, line 144
144:     def integer_select(method, options = {})
145:       options[:step] ||= 1
146:       choices = []; i = 0
147:       (options[:first]..options[:last]).each do |n|
148:         choices << n if i % options[:step] == 0
149:         i += 1
150:       end
151:                   select method, choices, options
152:     end
label(method, text = nil, options = {}) click to toggle source

Render a field label.

     # File lib/jqmobile_helpers/form_builder.rb, line 164
164:     def label(method, text = nil, options = {})
165:       colon = false if options[:colon].nil?
166:       options[:for] = options[:label_for]
167:       required = options[:required]
168: 
169:       # remove special options
170:       options.delete :colon
171:       options.delete :label_for
172:       options.delete :required
173: 
174:       text = @template.send(:h, text.blank?? method.to_s.humanize : text.to_s)
175:       text << ':'.html_safe if colon
176:       text << @template.content_tag(:span, "*", :class => "required") if required
177:       super
178:     end
multipart_date_select(method, options = {}) click to toggle source

This differs from the Rails-default date_select in that it submits three distinct fields for storage in three separate attributes. This allows for partial dates (eg, “1984” or “October 1984”). See FlexDate for storing and manipulating partial dates.

     # File lib/jqmobile_helpers/form_builder.rb, line 116
116:     def multipart_date_select(method, options = {})
117:       options[:include_blank] ||= false
118:       options[:start_year]    ||= 1801
119:       options[:end_year]      ||= Time.now.year
120:       options[:prefix]          = object_name # for date helpers
121:       options[:label_for]       = "#{object_name}_#{method}_y"
122:                   build_shell(method, options) do
123:         [['y', 'year'], ['m', 'month'], ['d', 'day']].map{ |p|
124:           i,j = p
125:           value = @object.send(method.to_s + '_' + i)
126:           options[:field_name] = method.to_s + '_' + i
127:           eval("@template.select_#{j}(#{value.inspect}, options)")
128:         }.join(' ')
129:                   end
130:     end
radio_buttons(method, choices, options = {}) click to toggle source

Render a set of radio buttons. Takes a method name, an array of choices (just like a select field), and an options hash.

    # File lib/jqmobile_helpers/form_builder.rb, line 45
45:     def radio_buttons(method, choices, options = {})
46:       choices.map!{ |i| i.is_a?(Array) ? i : [i] }
47:       build_shell(method, options, "radio_buttons_field") do
48:         choices.map{ |c| radio_button method, c[1], :label => c[0],
49:           :label_for => [object_name, method, c[1].to_s.downcase].join('_') }
50:       end
51:     end
submit(value = nil, options = {}) click to toggle source

Submit button with smart default text (if value is nil uses “Create” for new record or “Update” for old record).

     # File lib/jqmobile_helpers/form_builder.rb, line 157
157:     def submit(value = nil, options = {})
158:       value = (@object.new_record?? "Create" : "Update") if value.nil?
159:       build_shell(value, options, 'submit_button') { super }
160:     end
year_select(method, options = {}) click to toggle source

Year select field. Takes options :start_year and :end_year, and :step.

     # File lib/jqmobile_helpers/form_builder.rb, line 135
135:     def year_select(method, options = {})
136:       options[:first] = options[:start_year] || 1801
137:       options[:last]  = options[:end_year] || Date.today.year
138:       integer_select(method, options)
139:     end

Private Instance Methods

check_box_field_template(l = {}) click to toggle source

Render check box field template.

     # File lib/jqmobile_helpers/form_builder.rb, line 255
255:     def check_box_field_template(l = {})
256:             <div data-role="fieldcontain" id="#{l[:div_id]}" class="field">              #{l[:element]} #{l[:label]} #{l[:decoration]}<br />              #{"<p class=\"field_description\">#{l[:description]}</p>" unless l[:description].blank?}            </div>
257:     end
default_field_template(l = {}) click to toggle source

Render default field template for all the following field methods: “fields_for“, “label”, “text_field“, “password_field“, “hidden_field“, “file_field“, “text_area“, “search_field“, “telephone_field“, “phone_field“, “url_field“, “email_field“, “number_field“, “range_field“

Examples

  <%= jq_form_for(@post) do |f| %>
    <%= f.text_field :name %>
  <% end %>

  #  =>(Ommiting form_for results)
  <div data-role="fieldcontain" id="post_name_field" class="field"> 
      label for="post_name">Name</label><br /> 
    <input id="post_name" name="post[name]" size="30" type="text" /> 
    </div>
 

You can pass a few other options for your input fields.

Examples

  <%= f.text_field :name, :description => "Fill in your name", :required => true,
      :label => "Your Name", :decoration => 'basically a field' %>
  # => <div data-role="fieldcontain" id="post_name_field" class="field"> 
      label for="post_name">Your Name<span class="required">*</span></label><br /> 
    <input id="post_name" name="post[name]" size="30" type="text" />basically a field
    </div>
     # File lib/jqmobile_helpers/form_builder.rb, line 243
243:     def default_field_template(l = {})
244:             <div data-role="fieldcontain" id="#{l[:div_id]}" class="field">              #{l[:label]}<br />        #{l[:element]}#{l[:decoration]}        #{"<p class=\"field_description\">#{l[:description]}</p>" unless l[:description].blank?}            </div>
245:     end
habtm_check_boxes_field_template(l = {}) click to toggle source

Render a group of HABTM check boxes.

     # File lib/jqmobile_helpers/form_builder.rb, line 282
282:     def habtm_check_boxes_field_template(l = {})
283:             <div data-role="fieldcontain" id="#{l[:div_id]}" class="field">              #{l[:label]}<br />        <div class="habtm_check_boxes">#{l[:element]}</div>#{l[:decoration]}        #{"<p class=\"field_description\">#{l[:description]}</p>" unless l[:description].blank?}            </div>
284:     end
radio_button_choice_template(l = {}) click to toggle source

Render single radio button. Note that this is the only field template without an enclosing <div class="field"> because it is intended for use only within the radio_buttons_template (plural).

     # File lib/jqmobile_helpers/form_builder.rb, line 268
268:     def radio_button_choice_template(l = {})
269:             #{l[:element]} #{l[:label]}
270:     end
radio_buttons_field_template(l = {}) click to toggle source

Render a group of radio buttons.

     # File lib/jqmobile_helpers/form_builder.rb, line 276
276:     def radio_buttons_field_template(l = {})
277:       default_field_template(l)
278:     end
submit_button_template(l = {}) click to toggle source

Render submit button template.

     # File lib/jqmobile_helpers/form_builder.rb, line 294
294:     def submit_button_template(l = {})
295:             <div class="button">#{l[:element]}</div>
296:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.