Created
September 14, 2009 08:00
-
-
Save joecorcoran/186552 to your computer and use it in GitHub Desktop.
Multiple select helper for Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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