Ruby 使用Rails5和Actioncable实现实时更新的问题

Ruby 使用Rails5和Actioncable实现实时更新的问题,ruby,websocket,real-time,ruby-on-rails-5,actioncable,Ruby,Websocket,Real Time,Ruby On Rails 5,Actioncable,我正在尝试用action cable实现一个简单的rails fullstack todo应用程序。 不幸的是,action cable上的文档仍然让我感到困惑。 我试着参考DHH的例子并调整我的应用程序 当我使用一个浏览器创建任务,另一个浏览器查看任务是否自动更改时,它没有显示实时更新 我的环境: Mac OS X Sierra Rails 5.0.0.1 Ruby 2.4.0 在Gemfile中启用Redis并在端口运行6379 重新思考数据库 我实施了: app/assets/javasc

我正在尝试用action cable实现一个简单的rails fullstack todo应用程序。 不幸的是,action cable上的文档仍然让我感到困惑。 我试着参考DHH的例子并调整我的应用程序

当我使用一个浏览器创建任务,另一个浏览器查看任务是否自动更改时,它没有显示实时更新

我的环境:

  • Mac OS X Sierra
  • Rails 5.0.0.1
  • Ruby 2.4.0
  • 在Gemfile中启用Redis并在端口运行
    6379
  • 重新思考数据库
  • 我实施了:

    app/assets/javascripts/channels/index.coffee

    //= require cable
    //= require_self
    //= require_tree .
    
    this.App = {};
    
    App.cable = ActionCable.createConsumer();  
    
    app/assets/javascripts/channels/tasks.js

    App.tasks = App.cable.subscriptions.create('TasksChannel', {
      received: function(data) {
        $("#tasks").removeClass('hidden')
        return $('#tasks').append(this.renderTask(data));
      }
    });
    
    app/channels/tasks\u channel.rb

    class TasksChannel < ApplicationCable::Channel
      def subscribed
        stream_from "tasks"
      end
    
      def unsubscribed
        # Any cleanup needed when channel is unsubscribed
      end
    
      def create
        @task = Task.new(task_params)
    
        respond_to do |format|
          if @task.save
            ActionCable.server.broadcast 'tasks', task: @task.title
            format.html { redirect_to tasks_url, notice: 'Task was successfully created.' }
            format.json { render :show, status: :created, location: @task }
          else
            format.html { render :new }
            format.json { render json: @task.errors, status: :unprocessable_entity }
          end
        end
      end
    
    Rails.application.configure do
      config.action_cable.url = "ws://localhost:3000/cable"
    
    Rails.application.routes.draw do
      mount ActionCable.server => '/cable'
    
    app/views/layouts/application.html.erb:添加了

    config/cable.yml

    local: &local
      url: redis://localhost:6379
    
    development: *local
    test: *local
    
    config/environments/development.rb

    class TasksChannel < ApplicationCable::Channel
      def subscribed
        stream_from "tasks"
      end
    
      def unsubscribed
        # Any cleanup needed when channel is unsubscribed
      end
    
      def create
        @task = Task.new(task_params)
    
        respond_to do |format|
          if @task.save
            ActionCable.server.broadcast 'tasks', task: @task.title
            format.html { redirect_to tasks_url, notice: 'Task was successfully created.' }
            format.json { render :show, status: :created, location: @task }
          else
            format.html { render :new }
            format.json { render json: @task.errors, status: :unprocessable_entity }
          end
        end
      end
    
    Rails.application.configure do
      config.action_cable.url = "ws://localhost:3000/cable"
    
    Rails.application.routes.draw do
      mount ActionCable.server => '/cable'
    
    config/routes.rb

    class TasksChannel < ApplicationCable::Channel
      def subscribed
        stream_from "tasks"
      end
    
      def unsubscribed
        # Any cleanup needed when channel is unsubscribed
      end
    
      def create
        @task = Task.new(task_params)
    
        respond_to do |format|
          if @task.save
            ActionCable.server.broadcast 'tasks', task: @task.title
            format.html { redirect_to tasks_url, notice: 'Task was successfully created.' }
            format.json { render :show, status: :created, location: @task }
          else
            format.html { render :new }
            format.json { render json: @task.errors, status: :unprocessable_entity }
          end
        end
      end
    
    Rails.application.configure do
      config.action_cable.url = "ws://localhost:3000/cable"
    
    Rails.application.routes.draw do
      mount ActionCable.server => '/cable'
    
    app/views/tasks/index.html.erb

    class TasksChannel < ApplicationCable::Channel
      def subscribed
        stream_from "tasks"
      end
    
      def unsubscribed
        # Any cleanup needed when channel is unsubscribed
      end
    
      def create
        @task = Task.new(task_params)
    
        respond_to do |format|
          if @task.save
            ActionCable.server.broadcast 'tasks', task: @task.title
            format.html { redirect_to tasks_url, notice: 'Task was successfully created.' }
            format.json { render :show, status: :created, location: @task }
          else
            format.html { render :new }
            format.json { render json: @task.errors, status: :unprocessable_entity }
          end
        end
      end
    
    Rails.application.configure do
      config.action_cable.url = "ws://localhost:3000/cable"
    
    Rails.application.routes.draw do
      mount ActionCable.server => '/cable'
    
    我怀疑他有什么毛病

  • app/assets/javascripts/channels/tasks.coffee
  • app/views/tasks/index.html.erb
  • 但我真的不知道如何修复/调整它们