-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.rb
44 lines (37 loc) · 1.05 KB
/
form.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Form
attr_accessor :action, :method, :content, :submit, :is_file
def initialize(action='/',method='GET',content=[],submit={}, file=false)
@action = action
@method = method
@content = content
@submit = submit
@is_file = file
end
# '<form action="@action" method="@method">
# <input type="type1" value="value1" .. />
# ...
# <input type="submit" value="submit_value" ... />
# </form>'
def to_html
enctype = 'enctype="multipart/form-data"' if @is_file
enctype ||= ''
str = '<form action="' + @action + '" method="' + @method + '" ' + enctype + '>'
@content.each do |c|
if c.is_a?Hash
str += c[:content].to_s + '<input '
(c.keys - [:content]).each do |input|
str += input.to_s + '="' + c[input].to_s + '" '
end
str += " />\n"
elsif c.nil?
str += "<br />\n"
end
end
str += '<input type="submit" '
@submit.each do |k,v|
str += k.to_s + '="' + v.to_s + '" '
end
str += '/>'
str + '</form>'
end
end