Okay, here''s my question. I have data in a MySQL table and I want to display the data in the alphabetical order of each row''s title. Right now it displays it in the order it''s added. Is there a simple method I can add to make it switch to alphabetical order, similar to how the .reverse method? I''m really new, so this might not make sense... Here''s the code as I have it now in my list.rhtml: <table> <% for recipe in @recipes %> <tr> <td><h3><%= link_to h(recipe.title), :action => ''show'', :id => recipe %></h3> <p><%= h(recipe.description) %></p> </td> <td> <%= link_to ''Edit'', :action => ''edit'', :id => recipe %><br /> <%= link_to ''Delete'', :action => ''destroy'', :id => recipe %> </td> </tr> <% end %> </table> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
This should do it. <% for recipe in @recipes.sort{|a,b| a.title <=> b.title } %> Dave wrote:> Okay, here''s my question. I have data in a MySQL table and I want to > display the data in the alphabetical order of each row''s title. Right > now it displays it in the order it''s added. Is there a simple method I > can add to make it switch to alphabetical order, similar to how the > .reverse method? I''m really new, so this might not make sense... > > Here''s the code as I have it now in my list.rhtml: > > <table> > <% for recipe in @recipes %> > <tr> > <td><h3><%= link_to h(recipe.title), :action => ''show'', :id => > recipe %></h3> > <p><%= h(recipe.description) %></p> > </td> > <td> > <%= link_to ''Edit'', :action => ''edit'', :id => recipe %><br /> > <%= link_to ''Delete'', :action => ''destroy'', :id => recipe %> > </td> > </tr> > <% end %> > </table> > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
> Okay, here''s my question. I have data in a MySQL table and I want to > display the data in the alphabetical order of each row''s title. Right > now it displays it in the order it''s added. Is there a simple method I > can add to make it switch to alphabetical order, similar to how the > .reverse method? I''m really new, so this might not make sense...In your controller, change the line that looks like this: @recipes = Recipe.find(:all, .....) to include as an argument: :order => ''title'' That should do it.> > Here''s the code as I have it now in my list.rhtml: > > <table> > <% for recipe in @recipes %> > <tr> > <td><h3><%= link_to h(recipe.title), :action => ''show'', :id => > recipe %></h3> > <p><%= h(recipe.description) %></p> > </td> > <td> > <%= link_to ''Edit'', :action => ''edit'', :id => recipe %><br /> > <%= link_to ''Delete'', :action => ''destroy'', :id => recipe %> > </td> > </tr> > <% end %> > </table> > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---