ActionView::Helpers::FormBuilder
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
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
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 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
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
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
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 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
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
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“
<%= 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.
<%= 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
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
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.