簡體   English   中英

Rails 2.3.4中的json庫具有向后兼容性問題

[英]Backward compatibility issues with json libraries in rails 2.3.4

Rails 2.0.2中用於對象(例如具有ID和名稱的用戶模型對象)的to_json方法,用於以以下形式提供簡單輸出

{"id":"xyz","name":"hello"}

但是在將rails升級到2.3.4時,其輸出為

{"user":{"id":"xyz","name":"hello"}}

這破壞了我大部分的前端代碼。

有沒有辦法以與2.0.2中相同的方式獲得結果?

尋找你們的快速答案!!

您需要編輯文件config/initializers/new_rails_defaults.rb並將ActiveRecord::Base.include_root_in_json = true更改為false。 那應該做。 請參閱to_json文檔。

如果由於某種原因您沒有該文件,則這是默認的Rails 2.3.x應用程序中的外觀。

# Be sure to restart your server when you modify this file.

# These settings change the behavior of Rails 2 apps and will be defaults
# for Rails 3. You can remove this initializer when Rails 3 is released.

if defined?(ActiveRecord)
  # Include Active Record class name as root for JSON serialized output.
  ActiveRecord::Base.include_root_in_json = true

  # Store the full class name (including module namespace) in STI type column.
  ActiveRecord::Base.store_full_sti_class = true
end

# Use ISO 8601 format for JSON serialized times and dates.
ActiveSupport.use_standard_json_time_format = true

# Don't escape HTML entities in JSON, leave that for the #json_escape helper.
# if you're including raw json in an HTML page.
ActiveSupport.escape_html_entities_in_json = false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM