1

Inherited Resources seems to automatically set the status to 422 when a request fails validation and the response type is JSON. However, if the response type is html, the status is set to 200. Is there a way to tell inherited_resources to always return a 422 if the validation fails?

Cheers, Andy

cddr
  • 300
  • 2
  • 8
  • It's not in `inherited_resources` it seems that this is in Rails ActionPack. `inherited_resources` basically just calls respond_with and Rails take care of the rest. Besides monkey patching, I don't see an easy way to change the default behavior. – ybart Feb 26 '14 at 15:27

1 Answers1

0

Here is a monkey patch that you can put in your initializers (tested on Rails 4.0.3):

class ActionController::Responder
  def navigation_behavior_with_errors(error)
    if !get? && has_errors? && default_action
      render :action => default_action, status: :unprocessable_entity
      return
    end

    navigation_behavior_without_errors(error)
  end

  alias_method_chain :navigation_behavior, :errors
end
ybart
  • 876
  • 8
  • 23