# Dropdownlist in Rubyon Rails

**URL:** <https://www.ruby-forum.com/t/dropdownlist-in-rubyon-rails/179144>\
**Category:** Rails\
**Created:** [November 18, 2009, 12:40pm UTC](https://www.ruby-forum.com/t/dropdownlist-in-rubyon-rails/179144 "2009-11-18T12:40:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ladyhawk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@ladyhawk](https://www.ruby-forum.com/u/ladyhawk)\
**Post date:** [November 18, 2009, 12:40pm UTC](https://www.ruby-forum.com/t/dropdownlist-in-rubyon-rails/179144/1 "2009-11-18T12:40:26Z")

</div>

Hi,  
I have a model called Plan with fields plan\_id,Title,description. I have  
another model called Buy with fields title,description,Plan. I would  
like to create a “New” View for Buy where we have a dropdown listing all  
the Titles from Mediaplan. All the Plan:titles are unique and should  
come from database.  
I created a migration to create a Buy with a foreign key constraint to  
Plan.

```
create_table :buys do |t|
  t.integer :plan_id, :null=>false, :options =>
   "CONSTRAINT fk_buys_plans REFERENCES plans(id)"
  t.string :title, :null=>false
  t.text :description, :null=>false
  t.timestamps
end

```

How do I create a dropdown list in a Buy’s View with all the Plan titles  
?

Thanks!

---

<div class="post-metadata">

**Author:** ![ladyhawk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@ladyhawk](https://www.ruby-forum.com/u/ladyhawk)\
**Post date:** [November 18, 2009, 12:56pm UTC](https://www.ruby-forum.com/t/dropdownlist-in-rubyon-rails/179144/2 "2009-11-18T12:56:52Z")

</div>

use collection\_select tag for this

collection\_select(:buy, :plan\_id, Plan.find(:all), :id, :title, {:prompt  
=\> true})

Thanks  
Brijesh S.

---

<div class="post-metadata">

**Author:** ![ladyhawk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@ladyhawk](https://www.ruby-forum.com/u/ladyhawk)\
**Post date:** [November 18, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/dropdownlist-in-rubyon-rails/179144/3 "2009-11-18T13:18:53Z")

</div>

Brijesh S. wrote:

> use collection\_select tag for this
> 
> collection\_select(:buy, :plan\_id, Plan.find(:all), :id, :title, {:prompt  
> =\> true})
> 
> Thanks  
> Brijesh S.

Thanks for responding. I tried that, but it was not displayed as  
dropdown. Instead it showed up as plain text on the browser

collection\_select(:mediabuy, :mediaplan\_id, Mediaplan.find(:all), :id,  
:title, {:prompt=\> true})

Thanks!

---

<div class="post-metadata">

**Author:** ![ladyhawk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@ladyhawk](https://www.ruby-forum.com/u/ladyhawk)\
**Post date:** [November 19, 2009, 12:37pm UTC](https://www.ruby-forum.com/t/dropdownlist-in-rubyon-rails/179144/4 "2009-11-19T12:37:29Z")

</div>

Brijesh S. wrote:

> use collection\_select tag for this
> 
> collection\_select(:buy, :plan\_id, Plan.find(:all), :id, :title, {:prompt  
> =\> true})
> 
> Thanks  
> Brijesh S.

Thanks! It works now. I made a dumb mistake by not putting that  
collection\_select in \<%=%\>.

---

<div class="post-metadata">

**Author:** ![ladyhawk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@ladyhawk](https://www.ruby-forum.com/u/ladyhawk)\
**Post date:** [November 19, 2009, 7:51pm UTC](https://www.ruby-forum.com/t/dropdownlist-in-rubyon-rails/179144/5 "2009-11-19T19:51:34Z")

</div>

Lady H. wrote:

> Thanks! It works now. I made a dumb mistake by not putting that  
> collection\_select in \<%=%\>.

+1 if you take that DB access out of your view…

In the controller, put in a :

@plans = Plan.find(:all)

and swap the view code to:

\<%= collection\_select(:buy, :plan\_id, @plans, :id, :title, {:prompt =\>  
true}) %\>

to separate data acquisition from presentation.

---

<div class="post-metadata">

**Author:** ![ladyhawk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@ladyhawk](https://www.ruby-forum.com/u/ladyhawk)\
**Post date:** [November 22, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/dropdownlist-in-rubyon-rails/179144/6 "2009-11-22T15:11:32Z")

</div>

Ar Chron wrote:

> Lady H. wrote:
> 
> > Thanks! It works now. I made a dumb mistake by not putting that  
> > collection\_select in \<%=%\>.
> 
> +1 if you take that DB access out of your view…

Thanks I will try that 🙂
