Is there a good book that really shows you how to DRY up your code? Because I don''t want to repeat my code all over again for all the 26 alphabetical letters. Here is my code that I use for 1 letter for the controller Client: def index @clients = @clients = Client.find :all, :conditions => "firstname like ''M%''", :order => "firstname ASC" end Then on my View index.rhtml I have this: <html> <head> <title>Listing Clients with First Name starting with M</title> </head> <body> <table> <% @clients.each do |client| %> <tr> <td><%= link_to client.firstname, :action => ''show'', :id => client.id %></td> </tr> <%end%> </table> </body> </html> And after the </table> tag I would like to have a link like this: <%= link_to ''N'', :action => index :letter or something to represent my letter I wan to use%> So in my controller Client I would have: def index @clients = @clients = Client.find :all, :conditions => "firstname like ''?''", :order => "firstname ASC" end Thanks -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
<%= link_to "N", :action => "index", :letter => "N" %> controller: def index @clients = Client.find :all, :conditions => [ "firstname like ''?%''", params[:letter] ], :order => "firstname ASC" end Jason On 5/31/07, Matthew Lagace <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Is there a good book that really shows you how to DRY up your code? > > Because I don''t want to repeat my code all over again for all the 26 > alphabetical letters. > > Here is my code that I use for 1 letter for the controller Client: > > def index > @clients = @clients = Client.find :all, :conditions => "firstname like > ''M%''", :order => "firstname ASC" > end > > Then on my View index.rhtml I have this: > > <html> > <head> > <title>Listing Clients with First Name starting with M</title> > </head> > <body> > <table> > <% @clients.each do |client| %> > <tr> > <td><%= link_to client.firstname, :action => ''show'', :id => client.id > %></td> > </tr> > <%end%> > </table> > </body> > </html> > > And after the </table> tag I would like to have a link like this: > <%= link_to ''N'', :action => index :letter or something to represent my > letter I wan to use%> > > So in my controller Client I would have: > > def index > @clients = @clients = Client.find :all, :conditions => "firstname like > ''?''", :order => "firstname ASC" > end > > Thanks > > -- > 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?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> <%= link_to "N", :action => "index", :letter => "N" %> > > controller: > > def index > @clients = Client.find :all, :conditions => [ "firstname like ''?%''", > params[:letter] ], :order => "firstname ASC" > end > > JasonOk i''ve tried that, but when I click on my link it gives me this error: error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''M''%'') ORDER BY firstname ASC'' at line 1: SELECT * FROM clients WHERE (firstname like ''''M''%'' Thanks -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Oh its ok, I fixed it, thanks -- 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?hl=en -~----------~----~----~----~------~----~------~--~---