我正在用Rails制作一个JSON API,除了当我使用respond_with
自定义类(不是ActiveRecord继承的类)之外,它似乎可以正常工作。如何在Rails 3中使用自定义类的respond_with?
这里是我的类:
class JsonResponse
def initialize(data, status)
@data = data
@status = status
end
def as_json(options={})
{
:data => @data,
:status => @status
}
end
end
这是一个简单的响应包装。当我尝试这样做:
def create
unless(Match.find_by_user_id(params[:user_id]))
Match.create(:user_id => params[:user_id])
end
time_response = JsonResponse.new("5", "success")
respond_with(time_response)
end
我得到这个错误:
NoMethodError (undefined method `model_name' for JsonResponse:Class):
app/controllers/matches_controller.rb:9:in `create'
任何想法? respond_with
正在让我发疯。
做了一些清理,使其更具可读性和问题的标题。 – 2010-11-22 21:06:06
感谢您的帮助! – Danny 2010-11-22 21:17:24