Adding member action? (Rails)
I have the following routes defined:
resources :patients do
collection do
get 'import' => :new_import
post 'import' => :import
end
end
resources :course_enrollments, :only => [:index, :show] do
member do
get 'completed'
post 'complete_course_enrollment'
end
end
I want to make it possible to show course enrollments and add course enrollments for a patient. Should this route go as a member actions "/patients/:id/enrollments" and /patients/id:/add_enrollment?
Or should I nest the routes?
Below are the models:
class Patient < ActiveRecord::Base
belongs_to :user, :dependent => :destroy
has_many :enrollments, :dependent => :destroy
has_many :clients, :through => :enrollments
has_many :course_requests, :dependent => :destroy
has_many :course_enrollments, :dependent => :destroy
has开发者_开发知识库_many :courses, :through => :course_enrollments
has_many :quiz_attempts, :dependent => :destroy
has_many :patient_course_steps, :dependent => :destroy
has_many :survey_results, :dependent => :destroy
accepts_nested_attributes_for :user
accepts_nested_attributes_for :enrollments
attr_accessible :user_attributes, :client_ids, :enrollments_attributes, :insurance
validate :has_clients?
end
class CourseEnrollment < ActiveRecord::Base
belongs_to :patient
belongs_to :course
attr_accessible :patient_id, :course_id, :started, :completed, :last_viewed
validates_uniqueness_of :patient_id, :scope => :course_id
end
resources :patients do
resources :course_enrollments, :as => :enrollments
collection do
get 'import' => :new_import
post 'import' => :import
end
end
Will give you patient_course_enrollments_path
for /patients/:id/enrollments
as you wanted.
精彩评论