Skip to content

Instantly share code, notes, and snippets.

@joecorcoran
Created September 14, 2009 08:00
Show Gist options
  • Save joecorcoran/186552 to your computer and use it in GitHub Desktop.
Save joecorcoran/186552 to your computer and use it in GitHub Desktop.
Multiple select helper for Rails
def multi_select(id, name, objects, selected_objects, value, content = {})
options = ""
objects.each do |o|
selected = selected_objects.include?(o) ? " selected=\"selected\"" : ""
option_value = escape_once(o.send(value))
text = [option_value]
unless content[:text].nil?
text = []
content[:text].each do |t|
text << o.send(t)
end
text = text.join(" ")
end
bracket = []
unless content[:bracket].nil?
content[:bracket].each do |b|
bracket << o.send(b)
end
bracket = bracket.join(" ")
end
option_content = bracket.empty? ? "#{text}" : "#{text} (#{bracket})"
options << "<option value=\"#{option_value}\"#{selected}>#{option_content}</option>\n"
end
"<select multiple=\"multiple\" id=\"#{id}\" name=\"#{name}\">\n#{options}</select>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment