不允许请求源:http:// localhost:3001使用Rails5和ActionCable时

day*_*ker 34 ruby-on-rails ruby-on-rails-5 actioncable

尝试使用ActionCable时,Rails 5.0.0.beta2中的应用程序出现服务器问题.

使用localhost:3000可以正常工作,因为这是大多数ActionCable默认的.但是,如果我尝试在端口3001上运行rails服务器,它会给我Request origin not allowed: http://localhost:3001

ActionCable文档提到使用类似的东西ActionCable.server.config.allowed_request_origins = ['http://localhost:3001'],如果我把它放入其中对我有用config.ru

但这似乎是一个非常奇怪的地方.我觉得它应该能够进入初始化文件或我的development.rb环境配置文件.

为了进一步证明我应该允许它进入那里,该设置ActionCable.server.config.disable_request_forgery_protection = true可以忽略请求源,即使我将它包含在development.rb中也是如此.

为什么会ActionCable.server.config.disable_request_forgery_protection在development.rb中工作,但ActionCable.server.config.allowed_request_origins不能(但在config.ru中工作)?

这不是一个紧迫的问题,因为我有几种选择作为解决方法.我只是想知道我是否遗漏了一些明显的关于我认为这应该如何工作的东西.

dis*_*rly 52

你可以放入 Rails.application.config.action_cable.allowed_request_origins = ['http://localhost:3001']你的development.rb

有关更多信息,请参阅https://github.com/rails/rails/tree/master/actioncable#allowed-request-origins

  • 为我修复了Rails 5 RC1的相同问题,谢谢! (3认同)

mah*_*fuz 7

对于我的 flutter 应用程序,请求来源为零。因此,需要添加nil到列表中。

我已将这段代码添加到 中config/environments/development.rb,并且它有效!

config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/, /file:\/\/*/, 'file://', nil]
Run Code Online (Sandbox Code Playgroud)