Reputation: 17
I have two models:
class WorkPlan < ActiveRecord::Base
has_many :work_plan_tasks
end
class WorkPlanTask < ActiveRecord::Base
belongs_to :work_plan
end
My form and index actions all work as expected with the associations.
I'm trying to modify the show view for a work plan such that it displays the work plan tasks that belongs to the work plan in question.
I added the code below at the bottom of my show view for work plans, but it keeps blowing up and I've spent far too much time tinkering with the code.
Any help would be greatly appreciated.
<% @work_plan.work_plan_task in @work_plan_task %>
<%= work_plan_task.task_name %?
<% end %>
Upvotes: 1
Views: 68
Reputation: 16888
That's not generally how you loop through items in Rails views. Try this:
<% @work_plan.work_plan_tasks.each do |work_plan_task| %>
<%= work_plan_task.task_name %>
<% end %>
The each
call does what you expect: go through the array, passing the item via work_plan_task
into the loop.
If you wanted to do it the way you were writing it, you have the syntax backward.
<% for work_plan_task in @work_plan.work_plan_tasks %>
<%= work_plan_task.task_name %>
<% end %>
... though honestly I don't even know if that would work, I've never actually tried. :-)
Finally, notice that @work_plan_task
and work_plan_task
are different variables.
Upvotes: 3