# Module: BivouacHelpers::FormView [ "README", "AUTHORS", "COPYING", "lib/bivouac/helpers/view/goh/base.rb", "lib/bivouac/helpers/view/goh/form.rb", "lib/bivouac/helpers/view/goh/html.rb", "lib/bivouac/helpers/view/goh/sound.rb", "lib/bivouac/helpers/view/goh/scriptaculous.rb", "lib/bivouac/helpers/view/goh/javascript.rb", nil].each do JavaScriptGenerator.view_html BivouacHelpers.view_html BivouacHelpers::SoundView.view_html BivouacHelpers::ScriptAculoUsView.view_html BivouacHelpers::BaseView.view_html BivouacHelpers::JavaScriptView.view_html BivouacHelpers::HtmlView.view_html BivouacHelpers::FormView.view_html end |
bivouac/helpers/view/html
Adds AJAX autocomplete functionality to the text input field with the DOM ID specified by field_id.
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 6 6: def auto_complete_field( field_id, options = nil ) 7: if options.has_key?( :indicator ) 8: data = options[:indicator] 9: options[:indicator] = "indicator_#{field_id}" 10: span( data, :id => options[:indicator], :style => "display: none" ) 11: end 12: 13: choises_id = "choises_#{field_id}" 14: div( :id => choises_id, :class => "autocomplete" ) do; end 15: url = options.delete( :url ) 16: javascript_tag "new Ajax.Autocompleter( '#{field_id}', '#{choises_id}', '#{url}', #{options_for_javascript(options)} );" 17: end
Returns a button that‘ll trigger a JavaScript function using the onclick handler.
The function argument can be omitted in favor of an update_page block, which evaluates to a string when the template is rendered (instead of making an Ajax request first).
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 87 87: def button_to_function(name, *args, &block) 88: html_options = args.last.is_a?(Hash) ? args.pop : {} 89: function = args[0] || '' 90: 91: function = update_page(&block) if block_given? 92: 93: input( html_options.merge({ 94: :type => "button", :value => name, 95: :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" 96: })) 97: end
blog.codahale.com/2006/01/14/a-rails-howto-simplify-in-place-editing-with-scriptaculous/
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 20 20: def editable_content(options) 21: options[:content] = { :element => 'span' }.merge(options[:content]) 22: # options[:url] = {}.merge(options[:url]) 23: options[:ajax] = { :okText => "'Save'", :cancelText => "'Cancel'"}.merge(options[:ajax] || {}) 24: script = Array.new 25: script << "new Ajax.InPlaceEditor(" 26: script << " '#{options[:content][:options][:id]}'," 27: script << " '#{options[:url]}'," 28: script << " {" 29: script << options[:ajax].map{ |key, value| "#{key.to_s}: #{value}" }.join(", ") 30: script << " }" 31: script << ")" 32: 33: send( 34: options[:content][:element], 35: options[:content][:text], 36: options[:content][:options] 37: ) 38: 39: javascript_tag( script.join("\n") ) 40: end
Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular reloading POST arrangement. Even though it‘s using JavaScript to serialize the form elements, the form submission will work just like a regular submission as viewed by the receiving side. The options for specifying the target with :url and defining callbacks is the same as link_to_remote.
A "fall-through" target for browsers that doesn‘t do JavaScript can be specified with the :action/:method options on :html.
Example:
form_remote_tag :html => { :url => R(SomePlace) }
The Hash passed to the :html key is equivalent to the options (2nd) argument in the form_tag method.
By default the fall-through action is the same as the one specified in the :url (and the default method is :post).
form_remote_tag takes a block, like form_tag:
form_remote_tag :url => '/posts' do div do; input( :type => 'submit', :name => 'submit' :value => 'Save' ); end end
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 183 183: def form_remote_tag(options = {}, &block) 184: options[:form] = true 185: 186: options[:html] ||= {} 187: options[:html][:onsubmit] = 188: (options[:html][:onsubmit] ? options[:html][:onsubmit] + "; " : "") + 189: "#{remote_function(options)}; return false;" 190: 191: form_tag( options[:html].delete(:url), options[:html], &block ) 192: end
Starts a form tag that points the action to an url. The method for the form defaults to POST.
Examples:
Example:
form_tag R(Post) do div do; input( :type => 'submit', :name => 'submit' :value => 'Save' ); end end
Will output:
<form action="/posts" method="post"><input type="submit" name="submit" value="Save" /></div></form>
Options:
If "put", "delete", or another verb is used, a hidden input with name _method is added to simulate the verb over post.
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 133 133: def form_tag(url, options = {}, &block) 134: options[:enctype] = "multipart/form-data" if options.delete(:multipart) 135: options[:action] = url 136: 137: method_tag = false 138: 139: case method = options.delete(:method).to_s 140: when /^get$/i # must be case-insentive, but can't use downcase as might be nil 141: options[:method] = "get" 142: when /^post$/i, "", nil 143: options[:method] = "post" 144: else 145: options[:method] = "post" 146: method_tag = true 147: end 148: 149: if block_given? 150: form( options ) do 151: yield( ) 152: input( :type => "hidden", :name => "_method", :value => method ) if method_tag 153: end 154: else 155: form( options ) do; end 156: end 157: end
Returns a button input tag that will submit form using XMLHttpRequest in the background instead of regular reloading POST arrangement. options argument is the same as in form_remote_tag.
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 102 102: def submit_to_remote(name, value, options = {}) 103: options[:with] ||= 'Form.serialize(this.form)' 104: 105: options[:html] ||= {} 106: options[:html][:type] = 'button' 107: options[:html][:onclick] = "#{remote_function(options)}; return false;" 108: options[:html][:name] = name 109: options[:html][:value] = value 110: 111: input options[:html] 112: end
Autocomplete options :
:url : URL to call for autocompletion results :tokens: :frequency : :minChars : :indicator : When sending the Ajax request Autocompleter shows this :updateElement : Hook for a custom function called after the element has been updated (i.e. when the user has selected an entry). :afterUpdateElement : Hook for a custom function called after the element has been updated (i.e. when the user has selected an entry). :callback : This function is called just before the Request is actually made, allowing you to modify the querystring that is sent to the server. :parameters : If you need to send any additional parameters through your search form, add them here, in the usual {field: 'value',another: 'value'} or 'field=value&another=value' manner.
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 54 54: def text_field(field_name, value = "", options = {}) 55: autocomplete_options = nil 56: 57: if options.has_key?( :autocomplete ) 58: unless ["on", "off"].include?( options[:autocomplete] ) 59: autocomplete_options = Hash.new( ) 60: autocomplete_options[:url] = options.delete( :autocomplete ) 61: # options[:autocomplete] = "off" 62: end 63: end 64: 65: 66: [:tokens, :frequency, :minChars, :indicator, :updateElement, :afterUpdateElement, :callback, :parameters].each do |op| 67: if options.has_key?( op ) 68: if autocomplete_options.nil? 69: options.delete( op ) 70: else 71: autocomplete_options[op] = options.delete( op ) 72: end 73: end 74: end 75: 76: options = { :id => "default_#{Time.now.to_i}", :type => "text", :name => field_name, :value => value }.merge( options ) 77: input( options ) 78: auto_complete_field( options[:id], autocomplete_options ) unless autocomplete_options.nil? 79: end