{"id":122,"date":"2009-07-27T22:58:10","date_gmt":"2009-07-28T04:58:10","guid":{"rendered":"http:\/\/www.pervasivecode.com\/blog\/?p=122"},"modified":"2016-01-03T20:21:40","modified_gmt":"2016-01-04T04:21:40","slug":"tidier-http-error-responses-in-rails-controllers","status":"publish","type":"post","link":"http:\/\/www.pervasivecode.com\/blog\/2009\/07\/27\/tidier-http-error-responses-in-rails-controllers\/","title":{"rendered":"Tidier HTTP error responses in Rails controllers"},"content":{"rendered":"<p>Rails provides some flexible and fairly short controller methods for responding with an HTTP error code. Given that controllers are complicated enough by nature, I&#8217;m always looking for ways to DRY them up and make the code easy to understand. So here are some controller methods that make it really easy to provide correct HTTP error responses to clients.<br \/>\n<!--more--><\/p>\n<p>Put this in config\/initializers\/respond_with_http_errors.rb :<\/p>\n<pre>\r\nclass ActionController::Base\r\n\r\n  protected\r\n\r\n  # Display a \"400 Bad Request\" response based on a determination made by application logic.\r\n  def respond_bad_request(message = \"Your request was invalid.\", options = {})\r\n    respond_with_error(400, 'Bad Request', message, options)\r\n  end\r\n\r\n  # Display a \"403 Forbidden\" response based on a determination made by application logic.\r\n  def respond_forbidden(message = \"You are not authorized to access that document.\", options = {})\r\n    respond_with_error(403, 'Forbidden', message, options)\r\n  end\r\n\r\n  # Display a \"404 Not Found\" response based on a determination made by application logic.\r\n  def respond_not_found(message = \"The document you requested doesn't exist.\", options = {})\r\n    respond_with_error(404, 'Not Found', message, options)\r\n  end\r\n\r\n  def respond_with_error(status_code, status_name, message, options)\r\n    flash[:error] = message\r\n    defaults = { :status =&gt; status_code }\r\n    respond_to do |format|\r\n      format.html { defaults.merge!(:inline =&gt; \"&lt;h1&gt;#{status_name}&lt;\/h1&gt;&lt;p&gt;&lt;%=h flash[:error] %&gt;&lt;\/p&gt;\", :layout =&gt; true) }\r\n      format.json { defaults.merge!(:json =&gt; \"#{status_name}: #{message}\".to_json) }\r\n    end\r\n    render defaults.merge(options)\r\n  end\r\n\r\nend\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Rails provides some flexible and fairly short controller methods for responding with an HTTP error code. Given that controllers are complicated enough by nature, I&#8217;m always looking for ways to DRY them up and make the code easy to understand. So here are some controller methods that make it really easy to provide correct HTTP &hellip; <a href=\"http:\/\/www.pervasivecode.com\/blog\/2009\/07\/27\/tidier-http-error-responses-in-rails-controllers\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Tidier HTTP error responses in Rails controllers&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-122","post","type-post","status-publish","format-standard","hentry","category-ruby-on-rails"],"_links":{"self":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/122"}],"collection":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":12,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":337,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/122\/revisions\/337"}],"wp:attachment":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}