Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails select_tag alphabetical order

I have this select_tag

select_tag :id, options_for_select(Portal.all.collect{|p| [p.name, portal_datum_path(p.id)]}, [@portal.name, portal_datum_path(@portal)]), :onChange => "window.location=($(this).val());"

It allows a user to select a portal on which to see certain elements i want to display these portals in alphabetical order.

I tried ordering the :name in the controller but no win.

def index
  @portals = Portal.with_name_or_subdomain(params[:keyword]).order(:name).limit(100)
end

I looked at the rails docs there are no built in options within the select_tag itself is there some secret option that i should be using?

like image 972
TheLegend Avatar asked Feb 17 '23 23:02

TheLegend


2 Answers

after a hour all you have to do is .sort the array of options that are passed into the options_for_select. this is my hack its a fix but not very sexy

select_tag :id, options_for_select(Portal.all.collect{|p| [p.name, portal_datum_path(p.id)]}.sort, [@portal.name, portal_datum_path(@portal)]), :onChange => "window.location=($(this).val());"

hope that helps

like image 184
TheLegend Avatar answered Mar 04 '23 03:03

TheLegend


alter it to

select_tag :id, options_from_collection_for_select(portals,:name, :id, 1), :onChange => "window.location=($(this).val());"**strong text**
like image 39
tingel2k Avatar answered Mar 04 '23 03:03

tingel2k