2011-01-18 8 views
4

레일 3 응용아파치/mod_proxy 서버의 백그라운드에서 실행됩니다. 레일 응용 프로그램에서레일 3 접두어로 매개 변수가있는 리소스 경로

필수 접두사를 아파치에서:site_pin

을 존재하는 난 추상 내 접두사 다음이 내 내 routes.rb에서

ServerName example.com 

ProxyRequests Off 
<Proxy *> 
    Order deny,allow 
    Allow from all 
</Proxy> 

ProxyPass/http://localhost:3000/site/example/ 
ProxyPassReverse/http://localhost:3000/site/example/ 

<Location /> 
    Order allow,deny 
    Allow from all 
</Location> 

난 다음 한 :

resources :products 

#RESTful fix 
match 'site/:site_pin/:controller/', :action => 'index', :via => [:get] 
match 'site/:site_pin/:controller/new', :action => 'new', :via => [:get] 
match 'site/:site_pin/:controller/', :action => 'create', :via => [:post] 
match 'site/:site_pin/:controller/:id', :action => 'show', :via => [:get] 
match 'site/:site_pin/:controller/:id/edit', :action => 'edit', :via => [:get] 
match 'site/:site_pin/:controller/:id', :action => 'update', :via => [:put] 
match 'site/:site_pin/:controller/:id', :action => 'destroy', :via => [:delete] 

모든 것이 제대로 작동하지만 누군가이 수정 프로그램을 제거하고 더 나은 해결책을 제공합니다. routes.rb 더 깨끗한가요?

답변

17

scope은 매우 효과적입니다. 에 rake:routes을 실행 이제

scope 'site/:site_pin' do 
    resources :products 
end 

을, 다음과 같은 결과가 나타납니다 : : 당신이 당신의 routes.rb에 위의 무엇을 게시 교체

products GET /site/:site_pin/products(.:format)    {:controller=>"products", :action=>"index"} 
      POST /site/:site_pin/products(.:format)    {:controller=>"products", :action=>"create"} 
new_product GET /site/:site_pin/products/new(.:format)   {:controller=>"products", :action=>"new"} 
edit_product GET /site/:site_pin/products/:id/edit(.:format) {:controller=>"products", :action=>"edit"} 
    product GET /site/:site_pin/products/:id(.:format)   {:controller=>"products", :action=>"show"} 
      PUT /site/:site_pin/products/:id(.:format)   {:controller=>"products", :action=>"update"} 
      DELETE /site/:site_pin/products/:id(.:format)   {:controller=>"products", :action=>"destroy"} 

:site_pinparams[:site_pin]로 사용할 수 있습니다.

당연히 다른 리소스와 경로를 범위 블록에 추가 할 수 있습니다. 모두 접두사는 site/:site_pin입니다.

+2

지금 너무 쉽습니다! 고맙습니다!!! –

+0

반갑습니다. 내 대답이 당신의 문제를 해결했다면, 대답을 받아 들일 수 있습니다. 앞으로 다른 질문에 대한 답변을보다 쉽게 ​​얻을 수 있습니다. – vonconrad

+0

고마워. – Fivell