I have an Opportunity
model that has Activity
as a nested resource. on my opportunities/show
page, I have a list of activities for that opportunity and a form to add new activities. When I click "add activity" I get:
undefined method `activities' for nil:NilClass
Here is the error source:
# POST /activities.json
def create
@activity = @opportunity.activities.new(activity_params)
if @activity.save
redirect_to @opportunity, notice: 'Activity has been added'
else
I defined my Opportunity
model as having many Activities
and that my Activities belongs to
an Opportunity. Here are the relevant parts of my Activity
controller:
def create
@activity = @opportunity.activities.new(activity_params)
if @activity.save
redirect_to @opportunity, notice: 'Activity has been added'
else
redirect_to @opportunity, alert: 'Unable to add Activty'
end
end
And here is my views/activities/new code
<%= form_for ([@opportunity, @opportunity.activities.new]) do |f| %>
<div class="field">
<%= f.label "Date Assigned" %> <br />
<%= f.text_field :date_assigned %>
</div>
<div class="field">
<%= f.label "Date Due" %> <br />
<%= f.text_field :date_due %>
</div>
<div class="field">
<%= f.label "Description" %> <br />
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label "Status" %> <br />
<%= f.text_field :status %>
</div>
<div class="actions">
<%= f.submit 'Add' %>
</div>
<% end %>
My routes:
resources :opportunities do
resources :activities
end
thank you!!