Module: Sinatra::FormHelpers

Defined in:
lib/sinatra/formhelpers.rb

Instance Method Summary collapse

Instance Method Details

#area(obj, field, options = {}) ⇒ Object



33
34
35
36
# File 'lib/sinatra/formhelpers.rb', line 33

def area(obj, field, options={})
  content = @params[obj] ? @params[obj][field.to_s] : ""        
  tag :textarea, content, options.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, options={})
  single_tag :input, options.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="", options={})
  content = @params[obj] && @params[obj][field.to_s] == value ? "true" : ""       
  single_tag :input, options.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, options={})
  single_tag :img, options.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 = "", options={})
  tag :label, display.blank? ? field.to_s.titleize : display, options.merge(:for => "#{obj}_#{field}")
end

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, options={})
  tag :a, content, options.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, options={})
  #content = @params[obj] && @params[obj][field.to_s] == value ? "true" : ""    
  # , :checked => content   
  tag :input, "", options.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, options={})
  content = ""
  items.each do |i|
    content = content + tag(:option, i, { :value => i })
  end
  tag :select, content, options
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,options={})
  options ||= options.stringify_keys
  "<#{name.to_s} #{options.to_html_attrs} />"
end

#submit(obj, value = "", options = {}) ⇒ Object



42
43
44
# File 'lib/sinatra/formhelpers.rb', line 42

def submit(obj, value="", options={})
  single_tag :input, options.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,options={})
  with_opts = "<#{name.to_s} #{options.to_html_attrs}>#{content}</#{name}>"
  no_opts = "<#{name.to_s}>#{content}</#{name}>"
  options.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="", options={})
  content = @params[obj] ? @params[obj][field.to_s] : ""       
  id = 
  single_tag :input, options.merge(:type => "text", :id => field == "" ? obj : "#{obj}_#{field}", :name => field == "" ? obj : "#{obj}[#{field}]", :value => content)
end