- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在制作一个使用 mongomapper 的 Ruby Sinatra 应用程序,我的大部分响应都将采用 JSON 格式。
现在我遇到了许多与 JSON 相关的不同事情。
JSON
类:http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html ActiveSupport
JSON http://api.rubyonrails.org/classes/ActiveSupport/JSON.html因为我使用的是 MongoMapper,它使用 ActiveSupport
。我使用单一方法来处理响应:
def handleResponse(data, haml_path, haml_locals)
case true
when request.accept.include?("application/json") #JSON requested
return data.to_json
when request.accept.include?("text/html") #HTML requested
return haml(haml_path.to_sym, :locals => haml_locals, :layout => !request.xhr?)
else # Unknown/unsupported type requested
return 406 # Not acceptable
end
end
线路:
return data.to_json
当 data
是我的 MongoMapper
模型类之一的实例时工作:
class DeviceType
include MongoMapper::Document
plugin MongoMapper::Plugins::IdentityMap
connection Mongo::Connection.new($_DB_SERVER_CNC)
set_database_name $_DB_NAME
key :name, String, :required => true, :unique => true
timestamps!
end
我怀疑在这种情况下,to_json
方法来自ActiveSupport
的某处,并在mongomapper
中进一步实现框架。
我也使用相同的方法来处理错误。我使用的错误类是我自己的错误类之一:
# Superclass for all CRUD errors on a specific entity.
class EntityCrudError < StandardError
attr_reader :action # :create, :update, :read or :delete
attr_reader :model # Model class
attr_reader :entity # Entity on which the error occured, or an ID for which no entity was found.
def initialize(action, model, entity = nil)
@action = action
@model = model
@entity = entity
end
end
当然,在这个类的实例上调用to_json
时,是不行的。不是以一种完全合理的方式:显然这个方法实际上是定义的。我不知道它会从哪里来。从堆栈跟踪来看,显然是 activesupport
:
Unexpected error while processing request: object references itself
object references itself
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:75:in `check_for_circular_references'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:46:in `encode'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:246:in `block in encode_json'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:246:in `each'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:246:in `map'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:246:in `encode_json'
但是这个方法实际上是在我的类中的什么地方定义的呢?
我需要像这样重写我的类中的方法:
# Superclass for all CRUD errors on a specific entity.
class EntityCrudError < StandardError
def to_json
#fields to json
end
end
但我不知道如何进行。鉴于顶部提到的 3 种方式,哪种方式最适合我?
最佳答案
事实证明,我不需要做任何特别的事情。
我并没有很快怀疑到这一点,但问题是:
class EntityCrudError < StandardError
..
attr_reader :model # Model class
..
end
该字段包含有效模型类:
class DeviceType
..
..
end
这让循环引用。我现在只用 name 类替换它,这将满足我的目的。现在 to_json
不再提示了,我也很高兴:)
我仍然想知道所有这些 JSON 实现之间有什么区别。
关于ruby - 关于在 ruby sinatra 应用程序中使用 JSON 的方法的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16419075/
您在 Sinatra 应用程序中使用什么验证码? 我喜欢 Google 的 http://www.google.com/recaptcha ,但似乎不适用于 Sinatra(不过有一个 Rails 插
如何从命令行将 Sinatra 应用作为守护程序运行? 它正在使用瘦: ruby app.rb -p 3000 -e production 我不喜欢在 app.rb 本身中设置它。我想从命令行取消它。
我正在使用 Sinatra 在 Ruby 中编写一个小型 Web 服务。使用 http 基本身份验证(在生产中通过 https)控制对几乎所有内容的访问。 我想从要求授权中排除一个特定的目录。是否有捷
我曾经能够在启动 sinatra 应用程序的窗口中看到为 sinatra 发出的 http 请求。 我认为在 sinatra 升级后我再也看不到它们了,我不知道如何做到这一点。我不需要将它们记录到文件
带有 activerecord gem 的 Sinatra 项目发出警告: DEPRECATION WARNING: Currently, Active Record suppresses error
我正在玩 Sinatra,我想让我的一条路线不区分大小写。我尝试添加这样的路线: get "(?i)/tileflood/?" do end 但它与预期的/tileflood 的任何排列都不匹配。我在
如果我没有在 Sinatra 中明确设置 session 过期时间,那么默认的过期时间是多少? 最佳答案 Sinatra session 来自 Rack::Session;所以开箱即用它默认为不过期。
似乎没有关于如何使用 Sinatra 中的 Rack::Protection 为表单启用 CSRF 保护的文档。有人做过这个吗?我似乎不知道如何告诉 Sinatra 启用此功能。 最佳答案 Sinat
假设我有一条 Sinatra 路线: put '/data' do request.body.read # ... end 看起来整个 request.body 都被读入内存了。有没有办法在主
我正在 Sinatra 中运行一个经典风格的应用程序,我想获取该应用程序绑定(bind)到的 URL。例如,如果我在开发环境中启动它,我希望得到:http://localhost:4567/,而在生产
我正在尝试在我的 Mac 上安装以下 ruby gem: http://www.sinatrarb.com/intro.html 我收到以下消息: michael-rosarios-mac
我最近更新到了 Ruby 1.9.2 (RVM)、Sinatra 1.1 和 Passenger 3.0.0。我有一个简单的应用程序,由以下部分组成: # config.ru require 'rub
似乎没有关于如何使用 Sinatra 中的 Rack::Protection 为表单启用 CSRF 保护的文档。有人做过这个吗?我似乎不知道如何告诉 Sinatra 启用此功能。 最佳答案 Sinat
与 sinatra 应用程序相比,Rack 应用程序有哪些优势? 谢谢 最佳答案 我认为提问者想要将它保留在 Rack 上的优点。 考虑到 Sinatra 已经是典型 Web 应用程序的极薄和简约的贴
我最近创建了一个带有登录表单(无基本身份验证)的 Sinatra 应用程序。为了防止在用户登录之前访问该应用程序,我在适当的位置放置了一个 before block before do unles
我如何设置 Sinatra 以便公用文件夹中的静态文件与响应 Access-Control-Allow-Origin = "*"一起返回? 最佳答案 在这里查看这个问题:Sinatra OPTIONS
我有一个基于 Sinatra 的 REST 服务应用程序,我想从其中一个路由中调用其中一个资源,从而有效地将一个资源与另一个资源组合在一起。例如 get '/someresource' do ot
我有一个使用经典 Sinatra 的旧项目(不是模块化 我在互联网上能找到的唯一使用 Puma 的示例是使用模块化 Sinatra。 引用:http://www.sinatrarb.com/intro
问题是默认处理程序是在 中定义的。 Sinatra::ShowExceptions ,由 控制:raise_errors 配置设置,这个设置是全有或全无。如果 :raise_errors 关闭然后 错
我有以下 Sinatra 应用程序,我正在测试 error阻止但它似乎不起作用。 这是我的 sinatra 应用程序: require 'rubygems' require 'sinatra' err
我是一名优秀的程序员,十分优秀!