# Data doesn't show in dropdown

**URL:** <https://www.ruby-forum.com/t/data-doesnt-show-in-dropdown/123591>\
**Category:** Rails\
**Created:** [December 19, 2007, 12:22am UTC](https://www.ruby-forum.com/t/data-doesnt-show-in-dropdown/123591 "2007-12-19T00:22:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tdellaringa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tdellaringa](https://www.ruby-forum.com/u/tdellaringa)\
**Post date:** [December 19, 2007, 12:22am UTC](https://www.ruby-forum.com/t/data-doesnt-show-in-dropdown/123591/1 "2007-12-19T00:22:54Z")

</div>

I believe I’m missing some connection somewhere. I have a ‘workout’  
model/table with various fields. Then I made a ‘activity’ model/table  
because there will be a dropdown with activities the user can choose -  
as well as add their own activities to (so its a CRUD element).

So when I do new workout, I want the current items to show in a  
dropdown, plus give them the opportunity to add to the list (which I  
have not yet done).

My workout model is simply this:

class Workout \< ActiveRecord::Base  
has\_many :activities  
end

The Activity model is:

class Activity \< ActiveRecord::Base  
belongs\_to :workout  
end

I populated the the activities table with 3 rows (verified in the  
command line). Then in my form partial, I have this:

\<%= error\_messages\_for ‘workout’ %\>

Date  
\<%= date\_select 'workout', 'date' %\>

Activity  
\<%= select 'activity', 'value', 'Choose an Activity' %\>

The problem is I only get the “choose an activity” in the dropdown - but  
not the items in the table itself.

What am I missing?

---

<div class="post-metadata">

**Author:** ![tdellaringa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tdellaringa](https://www.ruby-forum.com/u/tdellaringa)\
**Post date:** [December 19, 2007, 4:00am UTC](https://www.ruby-forum.com/t/data-doesnt-show-in-dropdown/123591/2 "2007-12-19T04:00:20Z")

</div>

The usage of

> \<%= select ‘activity’, ‘value’, ‘Choose an Activity’ %\>

is incorrect. The proper usage would be something like:

\<%= select(‘activity’, ‘value’, {‘Choose an Activity’,  
0}.merge(Activity.find(:all).collect {|a| [a.value, a.id] })) %\>

However, this is dangerous having the “Choose an Activity” in there.  
I recommend simply using a blank, like so:

\<%= select(‘activity’, ‘value’, Activity.find(:all).collect {|a|  
[a.value, a.id] }, {:include\_blank =\> true}) %\>

## Hope this helps.

```
 Travis

```

On Dec 18, 5:22 pm, Tom D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![tdellaringa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tdellaringa](https://www.ruby-forum.com/u/tdellaringa)\
**Post date:** [December 19, 2007, 6:08am UTC](https://www.ruby-forum.com/t/data-doesnt-show-in-dropdown/123591/3 "2007-12-19T06:08:25Z")

</div>

That does help - I ended up using this:

\<%= select(‘activity’, ‘value’, Activity.find(:all).collect {|a|  
[a.name, a.id] }, {:include\_blank =\> true}) %\>

The name is the long version (not the value) so I switched it. But this  
gives me a blank dropdown, I really prefer to have the “choose” line in  
there. Can you explain why it is dangerous to have that in there?

---

<div class="post-metadata">

**Author:** ![tdellaringa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tdellaringa](https://www.ruby-forum.com/u/tdellaringa)\
**Post date:** [December 19, 2007, 7:16am UTC](https://www.ruby-forum.com/t/data-doesnt-show-in-dropdown/123591/4 "2007-12-19T07:16:14Z")

</div>

It’s dangerous simply because it requires you to add checks to make  
sure that the directional option wasn’t selected and to properly  
respond to that error. There’s no technical danger, just the danger  
of us programmers screwing up (which happens more often than not).

There’s a possibility that Rails automatically handles this error. If  
it does, then you’re free to use it, from a danger aspect. However, a  
usability study that I once read shows that users will fail/forget to  
input data into fields that are preloaded (I believe the study I am  
recalling was done in a compiled GUI application, but I expect that  
the same issues apply to Web usability)

Here’s the link to the Ruby on Rails API docs (Note, this is the  
current version 2.0.2 – if you haven’t upgraded, I suggest you do  
so):

```
 http://api.rubyonrails.com

```

And, the Ruby API docs:

```
http://ruby-doc.org/core/

```

And a ton of great Ruby programming docs:

```
http://www.ruby-doc.org/

```

And I believe the 2 APIs are also put onto your computer under the gem  
docs and the ruby docs respectively during install.

On Dec 18, 11:09 pm, Tom D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![tdellaringa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tdellaringa](https://www.ruby-forum.com/u/tdellaringa)\
**Post date:** [December 19, 2007, 6:09am UTC](https://www.ruby-forum.com/t/data-doesnt-show-in-dropdown/123591/5 "2007-12-19T06:09:14Z")

</div>

Also - is there a good reference online to look this kind of thing up?  
(i.e., the usage of select in a form)

---

<div class="post-metadata">

**Author:** ![tdellaringa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tdellaringa](https://www.ruby-forum.com/u/tdellaringa)\
**Post date:** [December 19, 2007, 1:44pm UTC](https://www.ruby-forum.com/t/data-doesnt-show-in-dropdown/123591/6 "2007-12-19T13:44:13Z")

</div>

Tom D. wrote:

> I believe I’m missing some connection somewhere. I have a ‘workout’  
> model/table with various fields. Then I made a ‘activity’ model/table  
> because there will be a dropdown with activities the user can choose -  
> as well as add their own activities to (so its a CRUD element).
> 
> So when I do new workout, I want the current items to show in a  
> dropdown, plus give them the opportunity to add to the list (which I  
> have not yet done).
> 
> My workout model is simply this:
> 
> class Workout \< ActiveRecord::Base  
> has\_many :activities  
> end
> 
> The Activity model is:
> 
> class Activity \< ActiveRecord::Base  
> belongs\_to :workout  
> end
> 
> I populated the the activities table with 3 rows (verified in the  
> command line). Then in my form partial, I have this:
> 
> \<%= error\_messages\_for ‘workout’ %\>
> 
> Date  
> \<%= date\_select 'workout', 'date' %\>
> 
> Activity  
> \<%= select 'activity', 'value', 'Choose an Activity' %\>
> 
> The problem is I only get the “choose an activity” in the dropdown - but  
> not the items in the table itself.
> 
> What am I missing?

#############################

hi,  
u have to use, ajax based collection\_select tag to show the items in  
dropdown list with “add new item”.

if u use this, all the added items to the table will come inside the  
dropdown and if u select “add new item” option,  
then u can create a new item…

r u getting?

if this is ur requirement, see the below modified code.

\<%= error\_messages\_for ‘workout’ %\>

Date  
\<%= date\_select 'workout', 'date' %\>

Activity  
\<%= collection\_select(:activity, :id, @activities, :id, :activity\_name, options = {:prompt =\> "Add New Activity"}, html\_options = { :onChange =\> "new Ajax.Updater('dd\_cities', '/admin/edit\_activity/' + this[this.selectedIndex].value, {asynchronous:true, evalScripts:true});" } ) %\>

Here,

1. :activity --\> ur model name
2. :id -\> column id for activity table.
3. @activities -\> it is an array containing all the activity table’s  
records.
4. :id -\> id for each record in array.
5. :activity\_name -\> the field “activity\_name” coming from array.
6. ‘/admin/edit\_activity/’ --\> for example, admin is ur controller and  
edit\_activity is ur method inside ur controller.
7. ‘dd\_cities’ --\> where ever u want see or populate the details of  
selected activity or creating a new activity, u need a form right.  
so copy ur form in side the below code, i.e., in place od dotted area.

......

r u getting…  
if u have any doubts or queries regarding this,  
u can mail me -\> removed\_email\_address@domain.invalid
