Die*_*cha 4 parameters routes ruby-on-rails ruby-on-rails-3
我的rails 3应用程序在Apache/mod_proxy服务器的后台运行.
在rails应用程序中存在强制性前缀 :site_pin
在Apache中,我有以下内容来抽象我的前缀:
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>
Run Code Online (Sandbox Code Playgroud)
在我的routes.rb中我有以下内容:
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]
Run Code Online (Sandbox Code Playgroud)
一切都以这种方式运行良好,但是任何人都有更好的解决方案来删除此修复并使routes.rb更干净?
von*_*rad 17
scope
对此非常有效.将您在routes.rb上面发布的内容替换为:
scope 'site/:site_pin' do
resources :products
end
Run Code Online (Sandbox Code Playgroud)
现在,运行rake:routes
,您应该看到以下输出:
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"}
Run Code Online (Sandbox Code Playgroud)
:site_pin
将作为params[:site_pin]
.
当然,您将能够将其他资源和路由添加到范围块中; 所有这些都将以前缀为前缀site/:site_pin
.