Data doesn't show in dropdown

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?

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

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?

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

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

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