I\'m trying to build an API in rails 4, and am having an issue where rails returns a 500 error instead of a 406 when using respond_to :json
and trying to access
In Rails 4, you need to pass a lambda to enforce the constraint on a route.
Unfortunately, this will NOT work because it will still try to serve up (or attempt to serve up) an html template since the format is an optional parameter:
resources :posts, constraints: { format: 'json' }
This DOES work (uses the lambda):
resources :posts, constraints: lambda { |req| req.format == :json }
See the second (final) note in this section of the Rails guide.