私は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
が私を夢中にしています。 respond_toと連携respond_withこのことができますが、私はrespond_toが表示されない場合、私はわからない
質問をより読みやすくタイトルにするために少しクリーンアップしました。 –
ご協力いただきありがとうございます! – Danny