about 4 years ago

routes的一些小技巧

routes.rb
resources :post, only: [:index]
resources :user, except: [:destroy]
routes.rb
resources :board, constraints: {subdomain: 'admin'}
constraints subdomain: 'api' do
  resources :post
  resources :board
end
# => http://api.example.com/posts 

用namespace分隔開也是一個做法

routes.rb
constraints subdomain: 'api' do
    namespace :api do
    resources :post
    resources :board
  end
end
# => http://api.example.com/api/posts 

更簡潔一點:

routes.rb
constraints subdomain: 'api' do
    namespace :api, path: '/' do
    resources :post
    resources :board
  end
end
# => http://api.example.com/posts 
← Nested Attributes with Strong Parameters Integrate Stripe Subscription in Rails →
 
comments powered by Disqus