0
問題是通過Web套接字使用ActionCable廣播數據。該錯誤似乎表明它來自創建方法。NoMethodError(未定義的方法`提取'爲零:NilClass)
錯誤消息
Rendered weight/_weight.html.erb (1.1ms)
[ActionCable] Broadcasting to weight: "<div class=\"row\">\n <div class=\"col-md-8 well\">\n <p>12.0 kg</p>\n <small>less than a minute</small>\n </div>\n</div>"
Completed 500 Internal Server Error in 25ms (Views: 7.4ms | ActiveRecord: 10.6ms)
NoMethodError (undefined method `fetch' for nil:NilClass):
app/controllers/weight_controller.rb:7:in `create'
Rendering /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb
Rendering /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/_source.text.erb
Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/_source.text.erb (0.8ms)
Rendering /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb
Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.9ms)
Rendering /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.1ms)
Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (25.0ms)
從controller.rb創建方法
def create
@weight = Weight.new(weight_params)
@weight.user_id = current_user.id
if @weight.save
ActionCable.server.broadcast "weight", render(partial: 'weight/weight', object: @weight)
else
flash[:danger] = "New Weight was not added!"
redirect_to current_user
end
end
private
def weight_params
params.require(:weights).permit(:weight)
end
我只是不能工作了什麼是返回零。信息在廣播重量方面正確的事實是正確的,這表明它已將其正確保存到數據庫中。想不到還有什麼執行'fetch'方法。
很確定網絡套接字設置正確。見下文。
配置/ application.rb中:
config.action_cable.mount_path = '/cable'
配置/ routes.rb中:
mount ActionCable.server => '/cable'
weight_channel.rb:
def subscribed
stream_from "weight"
end
weight.coffee:
received: (data) ->
$("#messages").prepend(data)
線是'weight_controller.rb'文件的第7行?你在哪裏(如果)調用'fetch'? (在模型的視圖中?在由'save'調用的驗證中)? – Myst
第7行是上述摘錄中的第5行。我沒有在任何地方使用抓取。 – will
爲了調試目的,考慮將'render'與'broadcast'分開,並將其放在兩條不同的行中。即:'tmp = render(partial:'weight/weight',object:@weight)'new line'ActionCable.server.broadcast「weight」,tmp' ...讓我們知道哪一行(哪個方法調用)引發新錯誤。 – Myst