Module: AnyView::Helpers::FormHelpers
- Defined in:
- lib/any_view/form_helpers.rb
Instance Method Summary collapse
-
#button_tag(caption, options = {}) ⇒ Object
Constructs a button input from the given options button_tag “Cancel”, :class => ‘clear’.
-
#check_box_tag(name, options = {}) ⇒ Object
Constructs a check_box from the given options check_box_tag :remember_me, :value => ‘Yes’.
-
#error_messages_for(record, options = {}) ⇒ Object
Constructs list html for the errors for a given object error_messages_for @user.
-
#field_set_tag(*args, &block) ⇒ Object
Constructs a field_set to group fields with given options field_set_tag(“Office”, :class => ‘office-set’) parameters: legend_text=nil, options={}.
-
#fields_for(object, settings = {}, &block) ⇒ Object
Constructs form fields for an object using given or default form_builder Used within an existing form to allow alternate objects within one form fields_for @user.assignment do |assignment| …
-
#file_field_tag(name, options = {}) ⇒ Object
Constructs a file field input from the given options file_field_tag :photo, :class => ‘long’.
-
#form_for(object, url, settings = {}, &block) ⇒ Object
Constructs a form for object using given or default form_builder form_for @user, ‘/register’, :id => ‘register’ do |f| …
-
#form_tag(url, options = {}, &block) ⇒ Object
Constructs a form without object based on options form_tag ‘/register’ do …
-
#hidden_field_tag(name, options = {}) ⇒ Object
Constructs a hidden field input from the given options hidden_field_tag :session_key, :value => “__secret__”.
-
#image_submit_tag(source, options = {}) ⇒ Object
Constructs a submit button from the given options submit_tag “Create”, :class => ‘success’.
-
#label_tag(name, options = {}, &block) ⇒ Object
Constructs a label tag from the given options label_tag :username, :class => ‘long-label’ label_tag :username, :class => ‘long-label’ do …
-
#password_field_tag(name, options = {}) ⇒ Object
Constructs a password field input from the given options password_field_tag :password, :class => ‘long’.
-
#radio_button_tag(name, options = {}) ⇒ Object
Constructs a radio_button from the given options radio_button_tag :remember_me, :value => ‘true’.
-
#select_tag(name, options = {}) ⇒ Object
Constructs a check_box from the given options options = [[‘caption’, ‘value’], [‘Green’, ‘green1’], [‘Blue’, ‘blue1’], [‘Black’, “black1”]] options = [‘option’, ‘red’, ‘yellow’ ] select_tag(:favorite_color, :options => [‘red’, ‘yellow’], :selected => ‘green1’) select_tag(:country, :collection => @countries, :fields => [:name, :code]).
-
#submit_tag(caption = "Submit", options = {}) ⇒ Object
Constructs a submit button from the given options submit_tag “Create”, :class => ‘success’.
-
#text_area_tag(name, options = {}) ⇒ Object
Constructs a text area input from the given options text_area_tag :username, :class => ‘long’.
-
#text_field_tag(name, options = {}) ⇒ Object
Constructs a text field input from the given options text_field_tag :username, :class => ‘long’.
Instance Method Details
#button_tag(caption, options = {}) ⇒ Object
Constructs a button input from the given options button_tag “Cancel”, :class => ‘clear’
156 157 158 159 |
# File 'lib/any_view/form_helpers.rb', line 156 def (, = {}) .reverse_merge!(:value => ) input_tag(:button, ) end |
#check_box_tag(name, options = {}) ⇒ Object
Constructs a check_box from the given options check_box_tag :remember_me, :value => ‘Yes’
127 128 129 130 |
# File 'lib/any_view/form_helpers.rb', line 127 def check_box_tag(name, ={}) .reverse_merge!(:name => name, :value => '1') input_tag(:checkbox, ) end |
#error_messages_for(record, options = {}) ⇒ Object
Constructs list html for the errors for a given object error_messages_for @user
58 59 60 61 62 63 64 65 66 |
# File 'lib/any_view/form_helpers.rb', line 58 def (record, ={}) return "" if record.blank? or record.errors.none? .reverse_merge!(:header_message => "The #{record.class.to_s.downcase} could not be saved!") = record.errors. error_items = .collect { |er| content_tag(:li, er) }.join("\n") error_html = content_tag(:p, .delete(:header_message)) error_html << content_tag(:ul, error_items, :class => 'errors-list') content_tag(:div, error_html, :class => 'field-errors') end |
#field_set_tag(*args, &block) ⇒ Object
Constructs a field_set to group fields with given options field_set_tag(“Office”, :class => ‘office-set’) parameters: legend_text=nil, options={}
48 49 50 51 52 53 54 |
# File 'lib/any_view/form_helpers.rb', line 48 def field_set_tag(*args, &block) = args. legend_text = args[0].is_a?(String) ? args.first : nil legend_html = legend_text.blank? ? '' : content_tag(:legend, legend_text) field_set_content = legend_html + capture_content(&block) concat_content content_tag('fieldset', field_set_content, ) end |
#fields_for(object, settings = {}, &block) ⇒ Object
Constructs form fields for an object using given or default form_builder Used within an existing form to allow alternate objects within one form fields_for @user.assignment do |assignment| … end fields_for :assignment do |assigment| … end
16 17 18 19 20 |
# File 'lib/any_view/form_helpers.rb', line 16 def fields_for(object, settings={}, &block) builder_class = configured_form_builder_class(settings[:builder]) fields_html = capture_content(builder_class.new(self, object), &block) concat_content fields_html end |
#file_field_tag(name, options = {}) ⇒ Object
Constructs a file field input from the given options file_field_tag :photo, :class => ‘long’
141 142 143 144 145 |
# File 'lib/any_view/form_helpers.rb', line 141 def file_field_tag(name, ={}) @_multi_enc_type = true .reverse_merge!(:name => name) input_tag(:file, ) end |
#form_for(object, url, settings = {}, &block) ⇒ Object
Constructs a form for object using given or default form_builder form_for @user, ‘/register’, :id => ‘register’ do |f| … end
6 7 8 9 10 |
# File 'lib/any_view/form_helpers.rb', line 6 def form_for(object, url, settings={}, &block) builder_class = configured_form_builder_class(settings[:builder]) settings[:builder] = builder_class.new(self, object) form_tag(url, settings, &block) end |
#form_tag(url, options = {}, &block) ⇒ Object
Constructs a form without object based on options form_tag ‘/register’ do … end
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/any_view/form_helpers.rb', line 24 def form_tag(url, ={}, &block) .reverse_merge!(:method => 'post', :action => url) @_multi_enc_type = .delete(:multipart) fake_method = hidden_form_method_field([:method]) real_method = real_method_for_fake([:method]) [:method] = real_method builder = .delete(:builder) inner_form_html = fake_method.to_s inner_form_html += if builder capture_content(builder, &block) else capture_content(&block) end [:enctype] = "multipart/form-data" if @_multi_enc_type @_multi_enc_type = nil concat_content content_tag('form', inner_form_html, ) end |
#hidden_field_tag(name, options = {}) ⇒ Object
Constructs a hidden field input from the given options hidden_field_tag :session_key, :value => “__secret__”
84 85 86 87 |
# File 'lib/any_view/form_helpers.rb', line 84 def hidden_field_tag(name, ={}) .reverse_merge!(:name => name) input_tag(:hidden, ) end |
#image_submit_tag(source, options = {}) ⇒ Object
Constructs a submit button from the given options submit_tag “Create”, :class => ‘success’
163 164 165 166 |
# File 'lib/any_view/form_helpers.rb', line 163 def image_submit_tag(source, ={}) .reverse_merge!(:src => image_path(source)) input_tag(:image, ) end |
#label_tag(name, options = {}, &block) ⇒ Object
Constructs a label tag from the given options label_tag :username, :class => ‘long-label’ label_tag :username, :class => ‘long-label’ do … end
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/any_view/form_helpers.rb', line 71 def label_tag(name, ={}, &block) .reverse_merge!(:caption => name.to_s.titleize, :for => name) = .delete(:caption) + ": " if block_given? # label with inner content label_content = + capture_content(&block) concat_content(content_tag(:label, label_content, )) else # regular label content_tag(:label, , ) end end |
#password_field_tag(name, options = {}) ⇒ Object
Constructs a password field input from the given options password_field_tag :password, :class => ‘long’
105 106 107 108 |
# File 'lib/any_view/form_helpers.rb', line 105 def password_field_tag(name, ={}) .reverse_merge!(:name => name) input_tag(:password, ) end |
#radio_button_tag(name, options = {}) ⇒ Object
Constructs a radio_button from the given options radio_button_tag :remember_me, :value => ‘true’
134 135 136 137 |
# File 'lib/any_view/form_helpers.rb', line 134 def (name, ={}) .reverse_merge!(:name => name) input_tag(:radio, ) end |
#select_tag(name, options = {}) ⇒ Object
Constructs a check_box from the given options options = [[‘caption’, ‘value’], [‘Green’, ‘green1’], [‘Blue’, ‘blue1’], [‘Black’, “black1”]] options = [‘option’, ‘red’, ‘yellow’ ] select_tag(:favorite_color, :options => [‘red’, ‘yellow’], :selected => ‘green1’) select_tag(:country, :collection => @countries, :fields => [:name, :code])
115 116 117 118 119 120 121 122 123 |
# File 'lib/any_view/form_helpers.rb', line 115 def select_tag(name, ={}) .reverse_merge!(:name => name) collection, fields = .delete(:collection), .delete(:fields) [:options] = (collection, fields) if collection [:options].unshift('') if .delete(:include_blank) = (.delete(:options), .delete(:selected)).join .merge!(:name => "#{[:name]}[]") if [:multiple] content_tag(:select, , ) end |
#submit_tag(caption = "Submit", options = {}) ⇒ Object
Constructs a submit button from the given options submit_tag “Create”, :class => ‘success’
149 150 151 152 |
# File 'lib/any_view/form_helpers.rb', line 149 def submit_tag(="Submit", ={}) .reverse_merge!(:value => ) input_tag(:submit, ) end |
#text_area_tag(name, options = {}) ⇒ Object
Constructs a text area input from the given options text_area_tag :username, :class => ‘long’
98 99 100 101 |
# File 'lib/any_view/form_helpers.rb', line 98 def text_area_tag(name, ={}) .reverse_merge!(:name => name) content_tag(:textarea, '', ) end |
#text_field_tag(name, options = {}) ⇒ Object
Constructs a text field input from the given options text_field_tag :username, :class => ‘long’
91 92 93 94 |
# File 'lib/any_view/form_helpers.rb', line 91 def text_field_tag(name, ={}) .reverse_merge!(:name => name) input_tag(:text, ) end |