Skip to content

Instantly share code, notes, and snippets.

@theam
Created November 17, 2009 16:19
Show Gist options
  • Save theam/237026 to your computer and use it in GitHub Desktop.
Save theam/237026 to your computer and use it in GitHub Desktop.
A couple of helpers to generate dynamic forms in AJAX based on radio selections
module SomeHelper
# form: Active form
# question: Text displayed as label
# method: The method to call
# options: A set of [value, label] pairs
def radio_choice(form, question, method, options, onchange_fn = nil)
html = question + "<br />"
options.each do |option|
html << form.radio_button(method, option[0], :onchange => onchange_fn)
html << form.label("#{method.to_s}_#{option[0]}", option[1])
end
html
end
# form: Active form
# question: Text displayed as label
# method: The method to call
# options: A set of [value, label] pairs
# condition: The value for which the block is displayed
# conditional_block: The block to display when value of method is condition
def radio_choice_with_conditional_block(form, question, method, options, condition, &conditional_block)
html = radio_choice(form, question, method, options, blind_checker("#{form.object.class.name.downcase}_#{method.to_s}_#{condition}","#{method.to_s}_#{condition}_options"))
html << "<div id='#{method.to_s}_#{condition}_options'"
unless form.object[method] && form.object[method]==condition
html << " style='display: none;' "
end
html << ">"
concat(html)
yield
concat "</div>"
end
private
def blind_checker(check_element, blind_element)
"if ($('" + check_element + "').checked) {" +
visual_effect(:blind_down, blind_element, :duration => 0.3) +
visual_effect(:highlight, blind_element) +
"}else{" +
visual_effect(:blind_up, blind_element, :duration => 0.3) +
"}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment