respond_to :json, :html
.
.
.
return_hash = {}
return_hash[:result] = "valid"
return_hash[:status] = "#{userName} has successfully registered for tournament #{tourneyID}"
respond_with(return_hash) #<--Throwing expection NoMethodError (undefined method `model_name' for NilClass:Class):
這裏的堆棧跟蹤...Rails 3:爲什麼從POST請求調用時,我的respond_to語句會拋出此異常?
NoMethodError (undefined method `model_name' for NilClass:Class):
app/controllers/tournaments_controller.rb:48:in `register'
Rendered /Users/myname/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.8ms)
Rendered /Users/myname/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (151.8ms)
Rendered /Users/myname/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (211.1ms)
非常感謝!
不知道它的問題,但我要補充這就是所謂的響應POST請求
更新: 我有類似的代碼工作正常,它看起來像這樣...
# Store the tourney data
tourney_hash[:tournament_count] = 1
tourney_hash[:tournament_id] = nextTourney.id
tourney_hash[:remaining_time_in_seconds] = remainingTimeInSeconds
respond_with(tourney_hash)
唯一不同的是這個代碼是從GET請求調用,而有問題的代碼上方從POST請求稱爲
更新: 當我更改此代碼,以便它從GET請求而不是POST請求調用時,它工作正常。你的想法?
UPDATE: 暫且,我把它換成聲明 「respond_with(return_hash)」 和 「渲染:JSON => return_hash.to_json」,它工作正常。不理想,壽。
爲了好奇的緣故,如果使用實例變量,它是否也會這樣做? – apneadiving 2011-04-25 17:23:14
是的,它的確如此。當我嘗試,並與存儲在它的10的值返回一個臨時實例變種,我得到這個例外,「NoMethodError(未定義的方法'模型名稱」的Fixnum對象:類): 應用程序/控制器/ tournaments_controller.rb:50:在'註冊」「 – BeachRunnerFred 2011-04-25 17:28:27
你如何定義'model_name'和'userName' – apneadiving 2011-04-25 17:31:59