Reputation: 47
There is an association with course and course instance. When I visit the url /courses/1/course_instances/new, I get the error below.
This is the error I got:
No route matches {:controller=>"course_instances", :course_id=>nil}
Models
Course:
class Course < ActiveRecord::Base
attr_accessible :code, :credits, :description, :hours, :id, :name, :pass_mark
has_many :course_instances, :dependent => :destroy
accepts_nested_attributes_for :course_instances
end
Course Instance
class CourseInstance < ActiveRecord::Base
attr_accessible :end_date, :id, :start_date
belongs_to :course
end
Routes
Sis::Application.routes.draw do
resources :courses do
resources :course_instances
end
root :to => 'home#index'
end
Course Instance Controller
class CourseInstancesController < ApplicationController
before_filter :find_course
def new
@course_instance = @course.course_instances.build
respond_to do |format|
format.html
end
end
def find_course
@course = Course.find(params[:course_id])
end
end
new.html.erb
<%= form_for ([@course, @course_instance]) do |f| %>
---- excluded for brevity ----
Rake Routes
new_course_course_instance GET /courses/:course_id/course_instances/new(.:format) course_instances#new
Upvotes: 0
Views: 127
Reputation: 47
Am not sure what I did but it started to work. I tried backtracking to recreate the error however I have been unsuccessful so far. Weird! Thanks for the help from everyone!
Upvotes: 0