Hi, I''m finally a happy developer of a Rails app that''s got a working drop- down. In short, I prefixed the array of Vendors (which all have integer IDs > 0) with an new Vendor with in ID of 0 and a string "Select ..." in the field that populates the drop down. I''ll use some kind of "before" command to report "selection req''d" if no valid selection had been made upon creating/saving/whatever. The code below works. Is there a better "Rails way"? Thanks in Advance, Richard <%= f.label :vendor %><br /> <% @vendors = Vendor.find( :all, :order=>"nickname ASC") -%> <% @v = Vendor.new; @v.id=0; @v.nickname = "--Select Vendor--" -%> <% @vendors.insert(0, @v) -%> <%= f.collection_select(:vendor_id, @vendors, :id, :nickname) %> -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jul-20 19:08 UTC
Re: Prefix a drop-down with "Select Item" -- Rails way?
RichardOnRails wrote:> Hi, > > I''m finally a happy developer of a Rails app that''s got a working drop- > down. In short, I prefixed the array of Vendors (which all have > integer IDs > 0) with an new Vendor with in ID of 0 and a string > "Select ..." in the field that populates the drop down. I''ll use some > kind of "before" command to report "selection req''d" if no valid > selection had been made upon creating/saving/whatever. > > The code below works. Is there a better "Rails way"?Yes. See below.> > Thanks in Advance, > Richard > > <%= f.label :vendor %><br /> > <% @vendors = Vendor.find( :all, :order=>"nickname ASC") -%>As I and others have told you numerous times: STOP DOING DATABASE ACCESS IN THE VIEW. You should never have Vendor.find in your views. Ever. This belongs in the controller (or model).> <% @v = Vendor.new; @v.id=0; @v.nickname = "--Select Vendor--" -%> > <% @vendors.insert(0, @v) -%> > <%= f.collection_select(:vendor_id, @vendors, :id, :nickname) %>Check the rdoc for collection_select again. Pay special attention to the :prompt option. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-Jul-20 19:15 UTC
Re: Re: Prefix a drop-down with "Select Item" -- Rails way?
On 20 July 2010 20:08, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> RichardOnRails wrote: >> <%= f.label :vendor %><br /> >> <% @vendors = Vendor.find( :all, :order=>"nickname ASC") -%> > > As I and others have told you numerous times: STOP DOING DATABASE ACCESS > IN THE VIEW. You should never have Vendor.find in your views. Ever. > This belongs in the controller (or model). > >> <% @v = Vendor.new; @v.id=0; @v.nickname = "--Select Vendor--" -%> >> <% @vendors.insert(0, @v) -%> >> <%= f.collection_select(:vendor_id, @vendors, :id, :nickname) %> >+1 to what Marnen says about keeping the DB out of the view. The ":prompt" should sort you out too, so no more to add there. But I will add another nag: please don''t use instance variables unless you *really need* instance variables. All those intermediate variables you''re setting in the view should just be plain old variables - no need for the @. Of course, it might be seen as a "personal preference" that I''m expressing. But instance variables have a specific purpose (and a higher cost) and by using them you''re attributing context to your variables that they don''t *need*, and that''s just plain confusing when hoping to figure out what the intention of the developer was when reading their code. NOI -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dave Aronson
2010-Jul-20 19:45 UTC
Re: Re: Prefix a drop-down with "Select Item" -- Rails way?
On Tue, Jul 20, 2010 at 15:15, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> +1 to what Marnen says about keeping the DB out of the view.+another. I would suggest that RichardOnRails brush up on MVC. -Dave -- Specialization is for insects. | Professional: http://davearonson.com -Robert Anson Heinlein | Programming: http://codosaur.us -------------------------------+ Leadership: http://dare2xl.com Have Pun, Will Babble! -me | Et Cetera: http://davearonson.net -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
RichardOnRails
2010-Jul-20 20:08 UTC
Re: Prefix a drop-down with "Select Item" -- Rails way?
> I would suggest that RichardOnRails brush up on MVC.Why don''t you say explicitly how I can avoid offending your sensibility about MVC? I know of no threshold of Rails expertise before posting questions on this newsgroup. I suspect that people with high levels of Rails respond to a question like mine: - in order to enlarge the "Rails community" - for: the joy of helping their fellow humans - with the expectation the their responses will bring the consulting opportunities - or heap scorn on those whom they perceive as inferior I''ll check back later on this thread. Richard On Jul 20, 3:45 pm, Dave Aronson <googlegroups2d...-BRiZGj7G2yRXqviUI+FSNg@public.gmane.org> wrote:> On Tue, Jul 20, 2010 at 15:15, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > +1 to what Marnen says about keeping the DB out of the view. > > +another. I would suggest that RichardOnRails brush up on MVC. > > -Dave > > -- > Specialization is for insects. | Professional:http://davearonson.com > -Robert Anson Heinlein | Programming: http://codosaur.us > -------------------------------+ Leadership: http://dare2xl.com > Have Pun, Will Babble! -me | Et Cetera: http://davearonson.net-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jul-20 20:22 UTC
Re: Prefix a drop-down with "Select Item" -- Rails way?
RichardOnRails wrote:>> I would suggest that RichardOnRails brush up on MVC. > > Why don''t you say explicitly how I can avoid offending your > sensibility about MVC?Why do you think Dave''s sensibilities were offended? There''s nothing in his post as I read it that would support that interpretation. Anyway, several of us *have* said explicitly that you shouldn''t be doing database access in the view. I hope you know enough about MVC to understand that that''s where the MVC pattern is being broken...> > I know of no threshold of Rails expertise before posting questions on > this newsgroup.There is none. But when some people here see someone doing something they''ve been repeatedly told not to do, they understandably get a bit impatient... Please calm down. I can''t speak for anyone else, but I''m trying to help, not insult. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dave Aronson
2010-Jul-20 20:24 UTC
Re: Re: Prefix a drop-down with "Select Item" -- Rails way?
On Tue, Jul 20, 2010 at 16:08, RichardOnRails <RichardDummyMailbox58407-gP6xRNRnnqSxhq/XJNNIW0EOCMrvLtNR@public.gmane.org> wrote:> I know of no threshold of Rails expertise before posting questions on > this newsgroup.Nope, nor even for posting answers, as I amply demonstrate. <:-)> I suspect that people with high levels of Rails respond to a question > like mine: > - in order to enlarge the "Rails community" > - for: the joy of helping their fellow humans > - with the expectation the their responses will bring the consulting > opportunities > - or heap scorn on those whom they perceive as inferiorI think you have read more into my post, than I intended. I certainly did not mean to heap scorn on you, only to help you realize what specific concept you seem to be failing to understand. Putting a name on it helps you find information about it. You seem to be in need of brushing up on MVC because you''re putting controller logic in your view, and apparently some people have corrected you on this (anti-)pattern several previous times. (Unfortunately, controller and view are not like chocolate and peanut butter....) Peace? Joy? Consultancy? ;-) -Dave -- Specialization is for insects. | Professional: http://davearonson.com -Robert Anson Heinlein | Programming: http://codosaur.us -------------------------------+ Leadership: http://dare2xl.com Have Pun, Will Babble! -me | Et Cetera: http://davearonson.net -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dave Aronson
2010-Jul-20 20:28 UTC
Re: Re: Prefix a drop-down with "Select Item" -- Rails way?
On Tue, Jul 20, 2010 at 16:22, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Why do you think Dave''s sensibilities were offended? There''s nothing > in his post as I read it that would support that interpretation.Thank you, my knight in shining armor! ;-) -Simon des Beaux Eaux, NAS, mka Dave -- Specialization is for insects. | Professional: http://davearonson.com -Robert Anson Heinlein | Programming: http://codosaur.us -------------------------------+ Leadership: http://dare2xl.com Have Pun, Will Babble! -me | Et Cetera: http://davearonson.net -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jul-21 15:21 UTC
Re: Re: Prefix a drop-down with "Select Item" -- Rails way?
Dave Aronson wrote:> On Tue, Jul 20, 2010 at 16:22, Marnen Laibow-Koser > <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> Why do you think Dave''s sensibilities were offended? �There''s nothing >> in his post as I read it that would support that interpretation. > > Thank you, my knight in shining armor! ;-)You''re most welcome. And I''m not a knight, just a troubadour...> > -Simon des Beaux Eaux, NAS, mka DaveBest, -- Ld. Ze''ev ben ''Arye, Troubadour alias Marcellus Wolfhurst mka Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
RichardOnRails
2010-Aug-03 02:17 UTC
Re: Prefix a drop-down with "Select Item" -- Rails way?
Hey Dave, I was "baby sitting" my 14-yo granddaughter for a week, so that kept me away from my project. Thinking about my project during some idle time today, I realized I should put my setup-code in the Vendor- controller. As I was about to that, I reread your post to and discovered you had provided an answer buried in a lot of distracting verbiage:> you''re putting controller logic in your viewThat''s the simple and direct answer to my question. I appreciate you providing it and wish that it had been provided as the first line of your response. Nevertheless, thank you for confirming what I thought about today. Best wishes, Richard On Jul 20, 4:24 pm, Dave Aronson <googlegroups2d...-BRiZGj7G2yRXqviUI+FSNg@public.gmane.org> wrote:> On Tue, Jul 20, 2010 at 16:08, RichardOnRails > > <RichardDummyMailbox58...-gP6xRNRnnqSxhq/XJNNIW0EOCMrvLtNR@public.gmane.org> wrote: > > I know of no threshold of Rails expertise before posting questions on > > this newsgroup. > > Nope, nor even for posting answers, as I amply demonstrate. <:-) > > > I suspect that people with high levels of Rails respond to a question > > like mine: > > - in order to enlarge the "Rails community" > > - for: the joy of helping their fellow humans > > - with the expectation the their responses will bring the consulting > > opportunities > > - or heap scorn on those whom they perceive as inferior > > I think you have read more into my post, than I intended. I certainly > did not mean to heap scorn on you, only to help you realize what > specific concept you seem to be failing to understand. Putting a name > on it helps you find information about it. You seem to be in need of > brushing up on MVC because you''re putting controller logic in your > view, and apparently some people have corrected you on this > (anti-)pattern several previous times. (Unfortunately, controller and > view are not like chocolate and peanut butter....) > > Peace? Joy? Consultancy? ;-) > > -Dave > > -- > Specialization is for insects. | Professional:http://davearonson.com > -Robert Anson Heinlein | Programming: http://codosaur.us > -------------------------------+ Leadership: http://dare2xl.com > Have Pun, Will Babble! -me | Et Cetera: http://davearonson.net-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.