Skip to content

Instantly share code, notes, and snippets.

@ktaragorn
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktaragorn/c21bc1c13fa245c11aac to your computer and use it in GitHub Desktop.
Save ktaragorn/c21bc1c13fa245c11aac to your computer and use it in GitHub Desktop.
Formtastic input for HTML5 datalist
# implementing http://www.w3schools.com/tags/tag_datalist.asp
class DatalistInput
include Formtastic::Inputs::Base
include Formtastic::Inputs::Base::Stringish
include Formtastic::Inputs::Base::Collections
def to_html
@name = input_html_options[:id].gsub("_id", "")
input_wrapping do
label_html <<
builder.text_field(method, input_html_options) << # standard input
data_list_html # append new datalist element
end
end
def input_html_options
super.merge(:list => html_id_of_datalist)
end
def html_id_of_datalist
"#{@name}_datalist"
end
def data_list_html
html = builder.template.options_for_select(collection)
# collection.map do |option|
# binding.pry
# builder.template.tag(:option, :value => option.to_s) # TODO not sure what to call on `option` to get the value
# end.join("\n")
builder.template.content_tag(:datalist,html,{:id => html_id_of_datalist}, false)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment