# Select\_tag

**URL:** <https://www.ruby-forum.com/t/select-tag/99514>\
**Category:** Rails\
**Created:** [July 23, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/select-tag/99514 "2006-07-23T18:43:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [July 23, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/select-tag/99514/1 "2006-07-23T18:43:00Z")

</div>

Hello,  
I’m creating a form that contains a select\_tag, I need to get options of  
that select tag pulled from a database table (I need to specify certain  
columns), how can this be done?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [July 23, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/select-tag/99514/2 "2006-07-23T18:54:36Z")

</div>

select\_tag options\_from\_collection\_for\_select(collection, value\_method,  
text\_method, selected\_value = nil)

check it out on [api.rubyonrails.com](http://api.rubyonrails.com)  
for more details;

you could go like this:  
@people = Person.find(:all)

\<%= select\_tag options\_from\_collection\_for\_select(@people, ‘name’, ‘id’,  
3) %\>

# the checked value would be person number three

hope it helps,

s

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [July 23, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/select-tag/99514/3 "2006-07-23T19:18:46Z")

</div>

shai wrote:

> select\_tag options\_from\_collection\_for\_select(collection, value\_method,  
> text\_method, selected\_value = nil)
> 
> check it out on [api.rubyonrails.com](http://api.rubyonrails.com)  
> for more details;
> 
> you could go like this:  
> @people = Person.find(:all)
> 
> \<%= select\_tag options\_from\_collection\_for\_select(@people, ‘name’, ‘id’,  
> 3) %\>
> 
> # the checked value would be person number three
> 
> hope it helps,
> 
> s

Hello shai,

I get a NoMethodError, here is my code

#Model:

@categories = Category.find(:all)

#View:

\<%= select\_tag options\_from\_collection\_for\_select(@categories, ‘name’,  
‘id’) %\>

Thanks

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [July 23, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/select-tag/99514/4 "2006-07-23T19:53:55Z")

</div>

AN@S wrote:

> shai wrote:
> 
> > select\_tag options\_from\_collection\_for\_select(collection, value\_method,  
> > text\_method, selected\_value = nil)
> > 
> > check it out on [api.rubyonrails.com](http://api.rubyonrails.com)  
> > for more details;
> > 
> > you could go like this:  
> > @people = Person.find(:all)
> > 
> > \<%= select\_tag options\_from\_collection\_for\_select(@people, ‘name’, ‘id’,  
> > 3) %\>
> > 
> > # the checked value would be person number three
> > 
> > hope it helps,
> > 
> > s
> 
> Hello shai,
> 
> I get a NoMethodError, here is my code
> 
> #Model:
> 
> @categories = Category.find(:all)
> 
> #View:
> 
> \<%= select\_tag options\_from\_collection\_for\_select(@categories, ‘name’,  
> ‘id’) %\>
> 
> Thanks

I use the acts\_as\_enumerated plugin for common “list of values”. I  
believe the example below, however, should still work for you:

\<%= select (:order, :order\_status, OrderStatus.all.collect {|s| [  
(s.value), s.value ]}, { :include\_blank =\> true }) %\>

Regards,

Michael

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [July 24, 2006, 11:12am UTC](https://www.ruby-forum.com/t/select-tag/99514/5 "2006-07-24T11:12:35Z")

</div>

AN@S wrote:

> > 
> 
> Hello shai,
> 
> I get a NoMethodError, here is my code
> 
> #Model:
> 
> @categories = Category.find(:all)

No. It should be in the controller (in the action the view is run from),  
alternatively in the view itself (but that would be bad form) as \<%-  
@categories = Category.find(:all) %\>

Regards,  
Henning K.
