The select helper wants an array of values in the form of
[[“display”,“value”],[“display”,“value”]], etc. (In your case the
“display” is the userlevel text and the value is the id of the user
level). There are different ways to do this, of course (welcome to the
world of rails). An easy way to get what you need is the following:
UserLevel.find(:all).collect {|ul| [ul.text, ul.id]}
This will return the array that the select tag wants. You can, of
course, modify the “find” call to filter, sort or otherwise adjust the
results.
Whether you put this in the controller…
@userlevels = UserLevel.find(:all).collect {|ul| [ul.text, ul.id]}
and then use this in the view:
<%= select(‘user’,‘level’,@userlevels) %>
OR just put it all in the view:
<%= select(‘user’,‘level’,UserLevel.find(:all).collect {|ul| [ul.text,
ul.id]}) %>
…is pretty much a matter of personal preference, to me, because this
is such a simple thing. But purists will say to do it the first way.
c.
Michael S. wrote:
First, I want to thank all of you who have been so kind as to answer my
questions.The lights are coming on now, and a lot of it that seemed like
magic is starting to make logical sense.
One more question re combo boxes.
I know the select tag is used in the view to present the combo box on
the screen. Is the “find” for the associated table (I’m looking at the
description of select in the rails framework documentation) don in the
view as well, or is it in the controller. An example would be that I
have a userlevels table that has an id and the name of the level, in the
users table, I have userleve_id. How would I set up the select tag to
display the text userlevel and store the id from the userlevels table.
Which parts are in the view and which in the controller?
thanks again
—Michael