Rails routes constraints not working as expected -


i asked this question earlier today wrapping routes default json format. have sworn working earlier may have been mistaken.

how come works:

resources :insurances, only: [:index, :show], :defaults => { :format => 'json' } 

but not:

constraints format: :json   resources :insurances, only: [:index, :show] end 

am missing basic on how constraints work?

constraints in block format check against request object, returns values strings. using following code same :defaults example - checking rake routes should show { :format => 'json' } option on each of resource routes.

constraints format: 'json'     resources :insurances, only: [:index, :show] end 

if you'd prefer use symbol instead of string format, can via lambda:

constraints format: lambda {|request| request.format.symbol == :json }     resources :insurances, only: [:index, :show] end 

source: http://guides.rubyonrails.org/routing.html#request-based-constraints


Comments