I''m trying to use collection_select to display a drop down box from a database table, the only problem I have is, insted of displaying :name I''d like to display a string which is a combination of all the columns from a specific row in a table. How should I go about this? <%= collection_select(:classified, :category_id, @categories, :id, :name) %> -- 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 -~----------~----~----~----~------~----~------~--~---
On Mar 22, 8:27 pm, Jonathan Ng <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m trying to use collection_select to display a drop down box from a > database table, the only problem I have is, insted of displaying :name > I''d like to display a string which is a combination of all the columns > from a specific row in a table. How should I go about this? > > <%= collection_select(:classified, :category_id, @categories, :id, > :name) %>That fourth parameter (where you have ":name") just specifies a method to call on your object. So you can put any method in there, not just a database attribute. In other words, you could have the following method in your mode: def summary "#{self.id}: #{self.name}" end and then pass :summary to the collection_select method, and it''ll use the values returned by that method to populate your options tags. Chris --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
coming from a PHP background all this is driving me crazy. but, i love rails :) thanks Chris! Chris Mear wrote:> On Mar 22, 8:27 pm, Jonathan Ng <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> I''m trying to use collection_select to display a drop down box from a >> database table, the only problem I have is, insted of displaying :name >> I''d like to display a string which is a combination of all the columns >> from a specific row in a table. How should I go about this? >> >> <%= collection_select(:classified, :category_id, @categories, :id, >> :name) %> > > That fourth parameter (where you have ":name") just specifies a method > to call on your object. So you can put any method in there, not just a > database attribute. > > In other words, you could have the following method in your mode: > > def summary > "#{self.id}: #{self.name}" > end > > and then pass :summary to the collection_select method, and it''ll use > the values returned by that method to populate your options tags. > > Chris-- 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 -~----------~----~----~----~------~----~------~--~---
Maybe I am doing this wrong, but I can''t seem to get this to work correctly? I defined summary my my controller file, and called :summary from the 4th parameter of the collection_select. Am I missing something? Chris Mear wrote:> On Mar 22, 8:27 pm, Jonathan Ng <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> I''m trying to use collection_select to display a drop down box from a >> database table, the only problem I have is, insted of displaying :name >> I''d like to display a string which is a combination of all the columns >> from a specific row in a table. How should I go about this? >> >> <%= collection_select(:classified, :category_id, @categories, :id, >> :name) %> > > That fourth parameter (where you have ":name") just specifies a method > to call on your object. So you can put any method in there, not just a > database attribute. > > In other words, you could have the following method in your mode: > > def summary > "#{self.id}: #{self.name}" > end > > and then pass :summary to the collection_select method, and it''ll use > the values returned by that method to populate your options tags. > > Chris-- 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 -~----------~----~----~----~------~----~------~--~---
Whoops, I mis-counted -- I think it''s actually the fifth parameter! Chris On 23 Mar, 01:52, Guest <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Maybe I am doing this wrong, but I can''t seem to get this to work > correctly? I defined summary my my controller file, and called :summary > from the 4th parameter of the collection_select. Am I missing > something? > > > > Chris Mear wrote: > > On Mar 22, 8:27 pm, Jonathan Ng <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > >> I''m trying to use collection_select to display a drop down box from a > >> database table, the only problem I have is, insted of displaying :name > >> I''d like to display a string which is a combination of all the columns > >> from a specific row in a table. How should I go about this? > > >> <%= collection_select(:classified, :category_id, @categories, :id, > >> :name) %> > > > That fourth parameter (where you have ":name") just specifies a method > > to call on your object. So you can put any method in there, not just a > > database attribute. > > > In other words, you could have the following method in your mode: > > > def summary > > "#{self.id}: #{self.name}" > > end > > > and then pass :summary to the collection_select method, and it''ll use > > the values returned by that method to populate your options tags. > > > Chris > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---