Rails Routes - Limiting the available formats for a resource

You must wrap those routes in a scope. Constraints unfortunately don't work as expected in this case.

This is an example of such a block...

scope :format => true, :constraints => { :format => 'json' } do
  get '/bar' => "bar#index_with_json"
end

More information can be found here: https://github.com/rails/rails/issues/5548


You just add constraints about format :

resources :photos, :constraints => {:format => /(js|json)/}

None of the above solutions worked for me. I ended up going with this solution:

post "/test/suggestions", to: "test#suggestions", :constraints => -> (req) { req.xhr? }

Found on https://railsadventures.wordpress.com/2012/10/07/routing-only-ajax-requests-in-ror/#comment-375