可能重复:
Override to_json in Rails 2.3.5导轨 “错误的参数数目(1 0)” 在to_json方法
LIB/responses.rb
module Responses
class Response
def to_json
JSON.pretty_generate(self)
end
end
class ErrorResponse < Response
def initialize(cause)
self[:type]="Error"
self[:casue]=cause
end
end
class DataResponse < Response
attr_accessor :data
end
end
这是由控制器使用:
response=Responses::DataResponse.new
response.data=someData
render :json => response
现在我在lib/responses.rb:3:in to_json
中收到错误wrong number of arguments (1 for 0)
。 为什么?没有任何参数传递给to_json
,这被render :json
隐式调用。那么我的错误在哪里?
并连接到这一个http://stackoverflow.com/questions/9557307/rails-3-json-model-to-json-or- json-model – phoet 2012-07-22 11:32:02