作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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):
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)
# 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)
最佳答案
由于 http://apidock.com/rails/ActionController/MimeResponds/respond_with
respond_with(*resources, &block) public
respond_with
方法接受资源同时
return_hash
是一个散列,而不是一个 ActiveRecord 对象。所以你的代码是错误的。它永远不会奏效。
respond_with(some_hash)
.什么是
respond_with
方法?
respond_with wraps a resource around a responder for default representation
# GET request
respond_with(whatever)
# same as
respond_to do |format|
format.html{ } # will render your_action_name.html.erb
end
# POST request
respond_with(whatever)
# is same as
respond_to do |format|
format.html{ redirect_to WHATEVER } # !!!!
end
respond_with
作品
respond_with
但不是别的。所以你的方法是错误的。这就是为什么你有一个错误。因为要
redirect_to return_hash
它试图获取它的
model_name
生成路径。
respond_to do |format|
format.json{ render :json => return_hash.to_json }
end
关于ruby-on-rails - rails 3 : Why might my respond_to statement throw this exception when called from a POST request?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5780856/
我是一名优秀的程序员,十分优秀!