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,
- :activity --> ur model name
- :id -> column id for activity table.
-
@activities -> it is an array containing all the activity table’s
records.
- :id -> id for each record in array.
- :activity_name -> the field “activity_name” coming from array.
- ‘/admin/edit_activity/’ --> for example, admin is ur controller and
edit_activity is ur method inside ur controller.
- ‘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]