简体   繁体   English

无法修改冻结的哈希。 Rails 4.1和SSE

[英]Can't modify frozen hash. Rails 4.1 and SSE

I am trying to implement SSE in rails 4.1.16 with ActionController::Live. 我正在尝试使用ActionController :: Live在rails 4.1.16中实现SSE。 But I am consistently getting Can't modify frozen hash error. 但是我一直在收到Can't modify frozen hash错误”。 I am getting this error in development. 我在开发中遇到此错误。 The code is - 该代码是-

def continuous_printer_energy_data
 response.headers['Content-Type'] = 'text/event-stream'
 sse = SSE.new(response.stream, event: 'time')
 begin
  loop do
    puts request.path
    if(request.path == '/energy/printer/continuous')
      sse.close
      break
    end
    Energy.uncached do
      a = Energy.last
      sse.write({ :data => a })
      sleep 1
    end

  end

 rescue Exception => e
  puts 'its a exception'
  logger.error e.backtrace.join("\n")
  sse.close

 ensure
  sse.close

end

end 结束

My error back trace is - 我的错误追溯信息是-

    RuntimeError - can't modify frozen Hash:
  actionpack (4.1.16) lib/action_dispatch/middleware/cookies.rb:309:in `[]='
  actionpack (4.1.16) lib/action_dispatch/middleware/cookies.rb:532:in `[]='
  actionpack (4.1.16) lib/action_dispatch/middleware/session/cookie_store.rb:110:in `set_cookie'
  rack (1.5.5) lib/rack/session/abstract/id.rb:350:in `commit_session'
  rack (1.5.5) lib/rack/session/abstract/id.rb:226:in `context'
  rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.1.16) lib/action_dispatch/middleware/cookies.rb:562:in `call'
  activerecord (4.1.16) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.1.16) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
  activerecord (4.1.16) lib/active_record/migration.rb:380:in `call'
  actionpack (4.1.16) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.1.16) lib/active_support/callbacks.rb:82:in `run_callbacks'
  actionpack (4.1.16) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.1.16) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  actionpack (4.1.16) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.1.16) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.16) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.16) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.16) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.16) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.16) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.16) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  request_store (1.3.1) lib/request_store/middleware.rb:9:in `call'
  actionpack (4.1.16) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.5) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.16) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  actionpack (4.1.16) lib/action_dispatch/middleware/static.rb:84:in `call'
  rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.16) lib/rails/engine.rb:514:in `call'
  railties (4.1.16) lib/rails/application.rb:144:in `call'
  rack (1.5.5) lib/rack/content_length.rb:14:in `call'
  puma (3.6.0) lib/puma/configuration.rb:225:in `call'
  puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
  puma (3.6.0) lib/puma/server.rb:415:in `process_client'
  puma (3.6.0) lib/puma/server.rb:275:in `block in run'
  puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'

Thanks in advance for the help. 先谢谢您的帮助。

I ran into this problem myself and, if you're using AngularJS (or similar) it might be because of the way the front end is handling the XFRF_TOKEN . 我自己遇到了这个问题,如果您使用的是AngularJS (或类似工具),则可能是由于前端处理XFRF_TOKEN的方式XFRF_TOKEN If that's the case, look into incorporating the angular_rails_csrf gem. 如果是这种情况,请考虑合并angular_rails_csrf gem。

More here: https://github.com/jsanders/angular_rails_csrf/issues/7 此处更多内容: https//github.com/jsanders/angular_rails_csrf/issues/7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM