You can do this many ways.
1) In the models: You can set the default order of all the models. Plus for the has_many associations, you could add an order clause: has_many :songs, :order => :title
2) In the controller: Instead of Genre.all, you would do Genre.order(:name), or instead of artist.songs, you would do artist.songs.order(:title)
Then the map just acts on the ordered collection.
]]>In your example, the elements come back in the order in which they are in the database (song1, then song2, then song3). Of course, in the “real world” the data would be more like “Good Vibrations”, “Surfer Girl”, and “I Get Around”. What if you want the select list to be in alphabetical order? Normally I would pull the data like @songs = Song.find(:all, :order => ‘title ASC’) – how would you do that with the .map?
]]>When I select the third element, it executes a render partial with a link_to , :controller => ‘controller_name’, :action => ‘action_name’, :parameter1 => params[:select_name1], :parameter2 => params[:select_name2]
Thanks again for a great learning tool.
]]>Thanks Foluso, updated post with the converted erb – at the bottom.
]]># app/views/home/update_artists.js.erb
$(‘#artists_select’).html(“”);
$(‘#songs_select’).html(“”);
# app/views/home/update_songs.js.erb
$(‘#songs_select’).html(“”);
For the index.html.erb, just wrap the lines starting with “=” with <% and %>
And instead of :javascript, just wrap with .
I’ve updated the post to have the erb example for that file.
]]>