Rails 5操作电缆不工作

时间:2017-11-08 01:09:12

标签: ruby ruby-on-rails-5 actioncable

我查看了多个教程,在创建新的Message记录时似乎无法从动作电缆接收通知。我所要做的就是在控制台中打印出响应,这样我知道它有效。我已经安装了redis但仍然没有运气。这是我的文件。任何帮助,将不胜感激。

cable.yml

redis: &redis
adapter: redis
url: redis://localhost:6379/1

production: *redis
development: *redis
test: *redis

的application.js

//= require jquery
//= require bootstrap-sprockets
//= require rails-ujs
//= require turbolinks
//= require_tree ./channels

cable.js

// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
//
//= require action_cable
//= require_self
//= require_tree ./channels

(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer();

}).call(this);

messages.js

//= require cable
//= require_self

(function() {
    // Subscrive to the class name of the channel
    App.messages = App.cable.subscriptions.create('MessagesChannel', {
    /**
     * Whenever this channel pushes content, it is received here
     */
    received: function(message) {
        console.log(message);

    }
  });
}).call(this);

通道/ messages.rb

class MessagesChannel < ApplicationCable::Channel
  def subscribed
    stream_from 'messages'
  end
end

模型/ message.rb

class Message < ApplicationRecord

    after_save :broadcast_save

    def broadcast_save
       ActionCable.server.broadcast 'messages', html: render_task
    end

    private
    def render_task
       ApplicationController.render(partial: 'messages/message', locals: { message: self })
    end
  end

application.html.erb head元素

 <head>
    <title>Test</title>
    <%= action_cable_meta_tag %>
    <%= csrf_meta_tags %>
    <%= javascript_include_tag "//static.twilio.com/libs/twiliojs/1.2/twilio.min.js" %>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

  </head>

出于测试目的,我只是在调用控制器操作时创建一条新消息。

class PhoneController < ApplicationController

    def index
        @tickets = []
        Message.create(body: "Test")
    end
end

rails的控制台输出

Started GET "/phone" for 75.65.92.24 at 2017-11-08 00:54:15 +0000
Cannot render console from 75.65.92.24! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PhoneController#index as HTML
   (0.1ms)  BEGIN
  SQL (0.7ms)  INSERT INTO "messages" ("body", "created_at", "updated_at") 
VALUES ($1, $2, $3) RETURNING "id"  [["body", "Test"], ["created_at", "2017-
11-08 00:54:15.192006"], ["updated_at", "2017-11-08 00:54:15.192006"]]
  Rendered messages/_message.html.erb (0.5ms)
[ActionCable] Broadcasting to messages: {:html=>"asdf"}
   (5.4ms)  COMMIT
  Rendering phone/index.html.erb within layouts/application
  Rendered phone/index.html.erb within layouts/application (0.8ms)
Completed 200 OK in 77ms (Views: 60.7ms | ActiveRecord: 6.3ms)

redis控制台输出(最后一行)

[37939] 08 Nov 00:49:19.158 * The server is now ready to accept connections on port 6379

1 个答案:

答案 0 :(得分:0)

好像你的问题就在这里:

  

无法从75.65.92.24渲染控制台!允许的网络:127.0.0.1,:: 1,127.0.0.0/127.255.255.255

您可能会发现此问题有用:

How to disable "Cannot Render Console from..." on Rails