Skip to content

Instantly share code, notes, and snippets.

@topgenorth
Created January 8, 2011 01:38
Show Gist options
  • Save topgenorth/770419 to your computer and use it in GitHub Desktop.
Save topgenorth/770419 to your computer and use it in GitHub Desktop.
should get a list of publishers, sort them by display name, and create a select with a default value as the first option
def sorted_publishers_select_tag(default_option = nil)
publishers = Publisher.accessible_by(current_user.ability)
publishers.sort!{ |a,b| a.display_name.downcase <=> b.display_name.downcase }
options_tag = options_from_collection_for_select(publishers, 'id', 'display_name', params[:publisher_id])
if default_option.nil?
options = {}
else
# after sorting we need to stick in the default value so it's at the top
options = {:include_blank => default_option }
end
content_tag :select, options_tag, {"name" => 'publisher_id', "id" => 'publisher_id'}.update(options.stringify_keys)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment