다음 질문은 경로에서 컨트롤러로 변수를 전달하는 것과 관련이 있습니다. 하나의 모델을 가진 Ruby on Rails (v 2.3.3) 앱이 있는데,이 뷰는 여러보기를 통해 사용자에게 전달됩니다. 현재의 솔루션은 다중 라우트에 의해 트리거되는 다중 컨트롤러를 사용합니다. 예를 들면 다음과 같습니다.레일에서 RESTful 라우팅을 통해 컨트롤러에 변수 전달
ActionController::Routing::Routes.draw do |map| # defines map
map.resource :simpsons, :only => [] do |b|
b.resources :episodes, :controller => "SimpsonsEpisodes"
end
map.resource :flintstones, :only => [] do |b|
b.resources :episodes, :controller => "FlintstonesEpisodes"
end
그러나 DRYness를 위해이 라우트를 동일한 컨트롤러로 작동시키고 싶습니다. 제어기가 경로 사이에서 구별되도록하기 위해 경로를 통해 변수를 전달하려고합니다. 예를 들어 :
case(type)
when "simpsons" then ... do something for the Simpsons ...
when "flintstones" then ... do something for the Simpsons ...
else .... do something for all episodes ....
end
내가 아닌 편안하고 라우팅 (map.with_options 등)이 작업을 수행하는 방법을 발견,하지만 난 좋겠 :
는map.resource :simpsons, :only => [] do |b|
b.resources :episodes, :controller => "Episodes", :type => "simpsons"
end
map.resource :flintstones, :only => [] do |b|
b.resources :episodes, :controller => "Episodes", :type => "flintstones"
end
그래서 컨트롤러에서 나는이 작업을 수행 할 수 map.resource()로 RESTful 경로를 사용하는 것을 선호합니다. 하나의 추악한 솔루션은 컨트롤러에서 요청 URI를 구문 분석하는 것일 수 있습니다.