`
orcl_zhang
  • 浏览: 242199 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Rails HTTP Status Code to Symbol Mapping

阅读更多
http状态码http://zh.wikipedia.org/zh/HTTP%E7%8A%B6%E6%80%81%E7%A0%81
rails目录下,actionpack/lib/action_controller/status_codes.rb
rails把状态码和状态信息完全对应起来了,非常好。
module ActionController
  module StatusCodes #:nodoc:
    # Defines the standard HTTP status codes, by integer, with their
    # corresponding default message texts.
    # Source: http://www.iana.org/assignments/http-status-codes
    STATUS_CODES = {
      100 => "Continue",
      101 => "Switching Protocols",
      102 => "Processing",

      200 => "OK",
      201 => "Created",
      202 => "Accepted",
      203 => "Non-Authoritative Information",
      204 => "No Content",
      205 => "Reset Content",
      206 => "Partial Content",
      207 => "Multi-Status",
      226 => "IM Used",

      300 => "Multiple Choices",
      301 => "Moved Permanently",
      302 => "Found",
      303 => "See Other",
      304 => "Not Modified",
      305 => "Use Proxy",
      307 => "Temporary Redirect",

      400 => "Bad Request",
      401 => "Unauthorized",
      402 => "Payment Required",
      403 => "Forbidden",
      404 => "Not Found",
      405 => "Method Not Allowed",
      406 => "Not Acceptable",
      407 => "Proxy Authentication Required",
      408 => "Request Timeout",
      409 => "Conflict",
      410 => "Gone",
      411 => "Length Required",
      412 => "Precondition Failed",
      413 => "Request Entity Too Large",
      414 => "Request-URI Too Long",
      415 => "Unsupported Media Type",
      416 => "Requested Range Not Satisfiable",
      417 => "Expectation Failed",
      422 => "Unprocessable Entity",
      423 => "Locked",
      424 => "Failed Dependency",
      426 => "Upgrade Required",

      500 => "Internal Server Error",
      501 => "Not Implemented",
      502 => "Bad Gateway",
      503 => "Service Unavailable",
      504 => "Gateway Timeout",
      505 => "HTTP Version Not Supported",
      507 => "Insufficient Storage",
      510 => "Not Extended"
    }

    # Provides a symbol-to-fixnum lookup for converting a symbol (like
    # :created or :not_implemented) into its corresponding HTTP status
    # code (like 200 or 501).
    SYMBOL_TO_STATUS_CODE = STATUS_CODES.inject({}) do |hash, (code, message)|
      hash[message.gsub(/ /, "").underscore.to_sym] = code
      hash
    end

    # Given a status parameter, determine whether it needs to be converted
    # to a string. If it is a fixnum, use the STATUS_CODES hash to lookup
    # the default message. If it is a symbol, use the SYMBOL_TO_STATUS_CODE
    # hash to convert it.
    def interpret_status(status)
      case status
      when Fixnum then
        "#{status} #{STATUS_CODES[status]}".strip
      when Symbol then
        interpret_status(SYMBOL_TO_STATUS_CODE[status] ||
          "500 Unknown Status #{status.inspect}")
      else
        status.to_s
      end
    end
    private :interpret_status

  end
end
分享到:
评论

相关推荐

    codemirror-rails:将CodeMirror与Rails一起使用

    Codemirror-rails 为您的Rails应用程序连接资产。 入门 如果您使用的是Bundler,则可以将codemirror-rails添加到Gemfile中: gem 'codemirror-rails' 或手动安装codemirror-rails gem: gem install codemirror-...

    rails 5 test prescriptions build a healthy codebase

    With Rails examples, use testing to enable your code to respond better to future change. Plus, see how to handle real-world testing situations. This new edition has been updated to Rails 5.2 and ...

    rails-code

    以上就是Rails开发的一些核心概念和工具,"rails-code"这个压缩包很可能包含了书中示例应用的所有源代码,可以帮助读者更好地理解书中讲解的技术点,并动手实践。通过阅读和分析这些代码,你可以加深对Rails的理解,...

    Rails Recipes英文版(清晰文字pdf+源码)

    Ruby三神书之一(其余的两本是Agile.Web.Development.with.Rails和Ruby For Rails,在我的资源列表也有) ... Owning "Rails Recipes" is like having the best Rails programmers sitting next to you while you code.

    wrox Professional Ruby on Rails_code.zip

    Rails的ActiveRecord组件允许开发者通过简单的ORM(Object-Relational Mapping)方式与数据库进行交互,而ActionController负责处理HTTP请求和响应,ActionView则生成HTML输出。此外,Rails还内置了测试框架,如...

    rails code

    《Rails代码》是基于敏捷Web开发的教程,主要聚焦在Ruby on Rails框架的第三个版本上。这个框架是用Ruby编程语言构建高效、可维护的Web应用程序的首选工具。本资源包含的是该书《Agile Web Development with Rails ...

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    Also new is Webpack coverage, to develop the front-end code for your Rails application. Create a usable and attractive login form using Bootstrap's styles, while ensuring the database table backing ...

    Ajax on Rails

    This book teaches intermediate to advanced web developers how to use both Ajax and Rails to quickly build high-performance, scalable applications without being overwhelmed with thousands of lines of ...

    rails2 code

    标题 "rails2 code" 暗示我们关注的是Ruby on Rails框架的第二个主要版本,Rails 2。Rails是一个流行的开源Web应用框架,基于Ruby语言,它遵循“约定优于配置”(Convention over Configuration)的设计理念,使得...

    rails指南 中文版

    1. **ActiveRecord**:这是Rails框架中的ORM(Object-Relational Mapping)组件,它负责处理数据库操作。通过ActiveRecord,开发者可以使用面向对象的方式来操作数据,而无需编写SQL语句。 2. **ActionController**...

    [Rails] Crafting Rails Applications (英文版)

    This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more ...

    Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf

    综上所述,《Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf》是一个全面的资源,无论你是Rails新手还是资深开发者,都能从中学习到如何为Rails应用编写高质量的测试。从理论到实践,从单元测试...

    Rails101_by_rails4.0

    《Rails101_by_rails4.0》是一本专注于Rails 4.0.0版本和Ruby 2.0.0版本的自学教程书籍,它定位于中文读者,旨在成为学习Rails框架的参考教材。Rails(Ruby on Rails)是一个采用Ruby语言编写的开源Web应用框架,它...

    Ruby on Rails实践

    例如,ActiveRecord是Rails中的一个关键组件,它提供了ORM(Object-Relational Mapping)功能,允许开发者用Ruby对象直接操作数据库,无需编写SQL语句。 在Rails中,路由(Routes)是连接URL与控制器动作的桥梁。...

    Rails项目源代码

    Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...

Global site tag (gtag.js) - Google Analytics