Skip to content

Instantly share code, notes, and snippets.

@dengsauve
Last active January 14, 2018 22:11
Show Gist options
  • Save dengsauve/f94bb035de2ff890920da839225b2718 to your computer and use it in GitHub Desktop.
Save dengsauve/f94bb035de2ff890920da839225b2718 to your computer and use it in GitHub Desktop.
Bootstrap 3.3 HTML Form Generator
# Bootstrap 3.3 HTML form Generator
def get_form_meta()
# Ask user for the form helper_method
puts "What method are you using for your form?"
form_method = gets.chomp!
# Ask user for the form method
puts "\nWhat action is the form performing? "
form_action = gets.chomp!
return form_method, form_action
end
def get_form_inputs()
input_names = []
input_labels = []
input_types = []
input_values = []
user_input = "choice"
counter = 1
# Until the user enters a blank...
until user_input == "QQ" do
# Ask user for the input's name
puts "\nEnter input #{counter} name"
user_input = gets.chomp!
break if user_input == ""
input_names << user_input
name = user_input
# Ask user for the input's label
title_label = name.sub(/^./, &:upcase)
puts "\nEnter input #{counter} label (default: #{title_label})"
user_input = gets.chomp!
if user_input == ""
input_labels << title_label
else
input_labels << user_input
end
# Ask user for the input's type
puts "\nEnter input #{counter} type (default: text)"
user_input = gets.chomp!
if user_input == ""
input_types << "text"
else
input_types << user_input
end
# Assume class="form-control"
# Ask user for input value
puts "\nEnter input #{counter} value (default: $#{name})"
user_input = gets.chomp!
if user_input == ""
input_values << "$#{name}"
else
input_values << user_input
end
counter += 1
puts "\n"
end
return input_names, input_labels, input_types, input_values
end
def make_string(form_method, form_action, input_names, input_labels, input_types, input_values)
html_form_string = ""
# Add open form tag with action and method
html_form_string += "<form method='#{form_method}' action='#{form_action}'>\n"
# Add all form inputs in groups
input_types.each_with_index do |type, index|
html_form_string += "\n\t<div class='form-group'>\n"
html_form_string += "\t\t<label for='#{input_names[index]}'>#{input_labels[index]}</label>\n"
html_form_string += "\t\t<input type='#{type}' name='#{input_names[index]}' class='form-control' value='#{input_values[index]}'>\n"
html_form_string += "\t</div>\n\n"
end
# Add a submit input tag
html_form_string += "\t<input type='submit' name='submit' class='btn btn-primary'>\n"
# close the form
html_form_string += "\n</form>"
return html_form_string
end
form_method, form_action = get_form_meta()
input_names, input_labels, input_types, input_values = get_form_inputs()
html_form_string = make_string(form_method, form_action, input_names, input_labels, input_types, input_values)
puts html_form_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment