2012-07-29 6 views
0

저는 Employee 모델, Skill 모델 및 Department 모델이있는 Rails 애플리케이션을 보유하고 있습니다.레일에 중첩 된 리소스를 사용하십시오.

class Employee < ActiveRecord::Base 
    belongs_to :department 
    has_and_belongs_to_many :skills 
    attr_accessible :email, :firstname, :name, :twitter 
end 

class Skill < ActiveRecord::Base 
    has_and_belongs_to_many :employees 
    attr_accessible :name 
end 

class Department < ActiveRecord::Base 
    attr_accessible :name 
end 

나는이 경로를 적어 내려고했지만이 곳에서 문제가 발생합니다.

은 내가 독립적으로 기술과 부서를 만들 수 있도록하려면, 그것은 그러나

resources :employees do 
    resource :department 
    resources :skills 
end 

할 말이 생각합니다. 나는 직원과 부서 및 기술을 '연결'할 수 있어야합니다. ..

을,하지만 난 등

/departments 
/skills 
/skills/new 

을 할 수 있도록하고 싶습니다 말했다처럼 노선이 같은 의미 (ID/부서 : ID/기술/직원// 직원 /를) 만들

나는

EmployeeList::Application.routes.draw do 

    resources :departments 
    resources :skills 

    resources :employees do 
    resource :department 
    resources :skills 
    end 
end 

을 할 수있는이 내가 원하는 경로로 날을 제공하지만 내 routes.rb 파일에 두 번 나열 자원을 가지고 정말 나쁜 관행처럼 보인다. 어떻게해야합니까?

답변

2

"나는 또한 기술과 부서를 독립적으로 만들 수 있기를 원한다. 직원과 부서 및 기술을 연결하기 만하면된다." 다음은 중첩 된 자원 imho의 경우는 분명하지 않습니다. 중첩 된 자원은 "주변"자원의 "내부"에만 존재할 수 있습니다. belongs_to 및 has_many와의 간단한 1 : n 관계가 원하는 경로 여야합니다. 따라서 routes.rb에

EmployeeList::Application.routes.draw do 
    resources :departments 
    resources :skills 
    resources :employees 
end 
관련 문제