我正在用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