Module: Sinatra::FormHelpers
- Defined in:
- lib/sinatra/formhelpers.rb
Instance Method Summary collapse
- #area(obj, field, options = {}) ⇒ Object
- #checkbox(obj, field, options = {}) ⇒ Object
- #hidden(obj, field = "", options = {}) ⇒ Object
- #image(src, options = {}) ⇒ Object
- #label(obj, field, display = "", options = {}) ⇒ Object
-
#link(content, href, options = {}) ⇒ Object
FormHelpers are a suite of helper methods built to make building forms in Sinatra a breeze.
- #radio(obj, field, value, options = {}) ⇒ Object
- #select(obj, field, items, options = {}) ⇒ Object
-
#single_tag(name, options = {}) ⇒ Object
standard single closing tags single_tag :img, :src => “images/google.jpg” => <img src=“images/google.jpg” />.
- #submit(obj, value = "", options = {}) ⇒ Object
-
#tag(name, content, options = {}) ⇒ Object
standard open and close tags EX : tag :h1, “shizam”, :title => “shizam” => <h1 title=“shizam”>shizam</h1>.
- #text(obj, field = "", options = {}) ⇒ Object
Instance Method Details
#area(obj, field, options = {}) ⇒ Object
33 34 35 36 |
# File 'lib/sinatra/formhelpers.rb', line 33 def area(obj, field, ={}) content = @params[obj] ? @params[obj][field.to_s] : "" tag :textarea, content, .merge(:id => "#{obj}_#{field}", :name => "#{obj}[#{field}]") end |
#checkbox(obj, field, options = {}) ⇒ Object
46 47 48 49 |
# File 'lib/sinatra/formhelpers.rb', line 46 def checkbox(obj, field, ={}) single_tag :input, .merge(:type => "checkbox", :id => "#{obj}_#{field}", :name => "#{obj}[#{field}]") end |
#hidden(obj, field = "", options = {}) ⇒ Object
66 67 68 69 |
# File 'lib/sinatra/formhelpers.rb', line 66 def hidden(obj, field="", ={}) content = @params[obj] && @params[obj][field.to_s] == value ? "true" : "" single_tag :input, .merge(:type => "hidden", :id => "#{obj}_#{field}", :name => "#{obj}[#{field}]") end |
#image(src, options = {}) ⇒ Object
38 39 40 |
# File 'lib/sinatra/formhelpers.rb', line 38 def image(src, ={}) single_tag :img, .merge(:src => src) end |
#label(obj, field, display = "", options = {}) ⇒ Object
23 24 25 |
# File 'lib/sinatra/formhelpers.rb', line 23 def label(obj, field, display = "", ={}) tag :label, display.blank? ? field.to_s.titleize : display, .merge(:for => "#{obj}_#{field}") end |
#link(content, href, options = {}) ⇒ Object
FormHelpers are a suite of helper methods built to make building forms in Sinatra a breeze.
link “jackhq”, “www.jackhq.com”
label :person, :first_name text :person, :first_name
area :person, :notes
etc.
19 20 21 |
# File 'lib/sinatra/formhelpers.rb', line 19 def link(content, href, ={}) tag :a, content, .merge(:href => href) end |
#radio(obj, field, value, options = {}) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/sinatra/formhelpers.rb', line 51 def radio(obj, field, value, ={}) #content = @params[obj] && @params[obj][field.to_s] == value ? "true" : "" # , :checked => content tag :input, "", .merge(:type => "radio", :id => "#{obj}_#{field}_#{value}", :name => "#{obj}[#{field}]", :value => value) end |
#select(obj, field, items, options = {}) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/sinatra/formhelpers.rb', line 58 def select(obj, field, items, ={}) content = "" items.each do |i| content = content + tag(:option, i, { :value => i }) end tag :select, content, end |
#single_tag(name, options = {}) ⇒ Object
standard single closing tags single_tag :img, :src => “images/google.jpg”
> <img src=“images/google.jpg” />
83 84 85 86 |
# File 'lib/sinatra/formhelpers.rb', line 83 def single_tag(name,={}) ||= .stringify_keys "<#{name.to_s} #{.to_html_attrs} />" end |
#submit(obj, value = "", options = {}) ⇒ Object
42 43 44 |
# File 'lib/sinatra/formhelpers.rb', line 42 def submit(obj, value="", ={}) single_tag :input, .merge(:type => "submit", :value => value == "" ? obj : value) end |
#tag(name, content, options = {}) ⇒ Object
standard open and close tags EX : tag :h1, “shizam”, :title => “shizam”
> <h1 title=“shizam”>shizam</h1>
74 75 76 77 78 |
# File 'lib/sinatra/formhelpers.rb', line 74 def tag(name,content,={}) with_opts = "<#{name.to_s} #{.to_html_attrs}>#{content}</#{name}>" no_opts = "<#{name.to_s}>#{content}</#{name}>" .blank? ? no_opts : with_opts end |
#text(obj, field = "", options = {}) ⇒ Object
27 28 29 30 31 |
# File 'lib/sinatra/formhelpers.rb', line 27 def text(obj, field="", ={}) content = @params[obj] ? @params[obj][field.to_s] : "" id = single_tag :input, .merge(:type => "text", :id => field == "" ? obj : "#{obj}_#{field}", :name => field == "" ? obj : "#{obj}[#{field}]", :value => content) end |