Prefix a drop-down with "Select Item" -- Rails way?

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) %>

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
[email protected]

On 20 July 2010 20:08, Marnen Laibow-Koser [email protected] wrote:

<%= 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

On Tue, Jul 20, 2010 at 15:15, Michael P. [email protected]
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

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
[email protected]

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 A. [email protected]

On Tue, Jul 20, 2010 at 16:08, RichardOnRails
[email protected] 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? :wink:

-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

Dave A. wrote:

On Tue, Jul 20, 2010 at 16:22, Marnen Laibow-Koser
[email protected] 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! :wink:

You’re most welcome. And I’m not a knight, just a troubadour…

-Simon des Beaux Eaux, NAS, mka Dave

Best,

Ld. Ze’ev ben 'Arye, Troubadour
alias Marcellus Wolfhurst
mka Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Tue, Jul 20, 2010 at 16:22, Marnen Laibow-Koser
[email protected] 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! :wink:

-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

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 view

That’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 A. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs