2

ActionController::StatusCodesRails 3 から削除されたようです。

次のようなHTTPステータスコードの同義語を使用しました

200 => :ok
404 => :not_found
500 => :internal_server_error

その他のコードについては、こちらを参照してください。

http://apidock.com/rails/ActionController/Base/render#254-List-of-status-codes-and-their-symbols

これらは Rails 3 のどこで見つけることができますか?

4

2 に答える 2

3

Ruby on Rails はRackを使用します。ステータス コードは Rack::Utils で定義されています。

HTTP_STATUS_CODES = {
  100  => 'Continue',
  101  => 'Switching Protocols',
  102  => 'Processing',
  200  => 'OK',
  201  => 'Created',
  ...
}

次に、それらを使用してシンボルを作成します (つまり:switching_protocols):

SYMBOL_TO_STATUS_CODE = Hash[*HTTP_STATUS_CODES.map { |code, message|
  [message.downcase.gsub(/\s|-/, '_').to_sym, code]
}.flatten]

コード全体はここで参照できます。

于 2011-04-04T18:31:51.680 に答える
2

エラーコードはaction_dispatch/middleware/show_exceptions.rb、シンボルが実際の例外にマップされている場所にあるようです:

  'ActionController::RoutingError'             => :not_found,
  'AbstractController::ActionNotFound'         => :not_found,
  'ActiveRecord::RecordNotFound'               => :not_found,
  'ActiveRecord::StaleObjectError'             => :conflict,
  'ActiveRecord::RecordInvalid'                => :unprocessable_entity,
  'ActiveRecord::RecordNotSaved'               => :unprocessable_entity,
  'ActionController::MethodNotAllowed'         => :method_not_allowed,
  'ActionController::NotImplemented'           => :not_implemented,
  'ActionController::InvalidAuthenticityToken' => :unprocessable_entity

ただし、100 ~ 400 の範囲のマッピングは Rails から削除されています。おそらく、Rack に既に存在しているためです。

于 2011-04-04T18:12:50.660 に答える