Rails 3 Resource Route with Parameter as Prefix

My rails 3 app runs on the background of the Apache / mod_proxy server .

The rails application has a prefix :site_pin

In Apache, I have the following to distract my prefix:

 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> 

In my my .rb routes, I have the following:

 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] 

Everything works fine this way, but does anyone have a better solution to remove this patch and make route.rb cleaner?

+4
source share
1 answer

scope would be very effective for this. Replace what you placed above on your .rb routes with:

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

Now run rake:routes and you will see the following output:

  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_pin will be available as params[:site_pin] .

Naturally, you can add other resources and routes to the block of visibility; all of which will be prefixed with site/:site_pin .

+17
source

Source: https://habr.com/ru/post/1336036/


All Articles