javascript - Ruby on Rails、ActionCable、消息不显示

标签 javascript ruby-on-rails ruby actioncable

感谢您提前提供帮助!

我是 Rails 新手,一直在尝试遵循本指南:https://www.youtube.com/watch?v=tyNepRO_ERc&feature=youtu.be

过去,我在学校繁忙之前就实现了 ActionCable,当时也遇到过一些麻烦,而现在它又给我带来了麻烦。我在该视频中到达 17:00 分钟,没有任何问题,一切都有意义 - 但是,当我发送命令时,它不会显示任何消息。

所以,我所看到的是:

显示了“hello world”,但没有任何给定的输入(甚至在重新加载页面时也没有)

首先在我的终端中:

于 2016-05-23 21:45:00 -0600 开始 GET "/cable"for::1

于 2016-05-23 21:45:00 -0600 开始为::1 获取“/cable/”[WebSocket]

成功升级到 WebSocket(REQUEST_METHOD:GET、HTTP_CONNECTION:升级、HTTP_UPGRADE:websocket)

RoomChannel 正在传输订阅确认

RoomChannel 正在从 room_channel 进行流式传输

RoomChannel#speak({"message"=>"特拉维斯:很好!"})

[ActionCable] 向 room_channel 广播:“Travis:Neat!”

RoomChannel 传输“Travis:Neat!” (通过 room_channel 流式传输)

网络控制台:

App.room.speak('特拉维斯:很好!')

正确

我可以发布一些代码,我应该发布一些代码,但我相当确定我的所有代码都遵循视频。我仍然会整理一段时间,我只是更多地寻找任何提示/见解/帮助来检查其他想法/事情。我之前尝试实现 ActionCable 时也遇到过类似的问题,消息甚至会被发送并处理,但直到重新加载网页才会显示。

感谢您的帮助,

...好吧,我想我最好先放一些代码:

javascripts/channels/room.coffee

App.room = App.cable.subscriptions.create "RoomChannel",
# Called when the subscription is ready for use on the server
connected: ->

# Called when the subscription has been terminated by the server
disconnected: ->

# Called when there's incoming data on the websocket for this channel
received: (data) ->
$('#messages').append "<div>#{data}</div>"

speak: (message) ->
@perform 'speak', message: message

javascripts/cable.coffee

#= require action_cable
#= require_self
#= require_tree ./channels
#
@App ||= {}
App.cable = ActionCable.createConsumer()

channel /room_channel.rb

class RoomChannel < ApplicationCable::Channel
  def subscribed
    stream_from "room_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end

  def speak(data)
    ActionCable.server.broadcast 'room_channel', data['message']
  end
end

/controllers/rooms_controller.rb

class RoomsController < ApplicationController
  def show
    @messages = Message.all
  end
end

/models/message.rb

class Message < ApplicationRecord
    after_create_commit { BroadcastMessageJob.perform_later self  }
end

/views/messages/_message.html.erb

<p> <%= message.content %> </p>

/views/rooms/show.html.erb

<h1>ActionCable Chat</h1>

    <%= render @messages %>

/config/environments/development.rb (这里我插入了这一行,这实际上解决了我几个月前上次尝试使用 ActionCable 时的问题)

ActionCable.server.config.disable_request_forgery_protection = true

/config/cable.yml

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

/config/database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000
development:
  <<: *default
  database: [InsertNameOfMyAppHere]_development

老实说,我知道,我的 Gemfile 一团糟:

   ruby '2.3.1'
    source 'https://rubygems.org'
    
    
    # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
    gem 'rails','>= 5.0.0.rc1'
    gem 'pg'
    # Use sqlite3 as the database for Active Record
    
    #gem 'sqlite3'
    # Use SCSS for stylesheets
    gem 'sass-rails', '~> 5.0'
    # Use Uglifier as compressor for JavaScript assets
    gem 'uglifier', '>= 1.3.0'
    # Use CoffeeScript for .coffee assets and views
    gem 'coffee-rails', '~> 4.1'
    # See https://github.com/rails/execjs#readme for more supported runtimes
    # gem 'therubyracer', platforms: :ruby
    
    # Use jquery as the JavaScript library
    gem 'jquery-rails'
    # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
    gem 'turbolinks'
    # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
    gem 'jbuilder', '~> 2.0'
    # bundle exec rake doc:rails generates the API under doc/api.
    gem 'sdoc', '~> 0.4.0', group: :doc
    
    # Use ActiveModel has_secure_password
    # gem 'bcrypt', '~> 3.1.7'
    
    # Use Unicorn as the app server
    # gem 'unicorn'
    
    # Use Capistrano for deployment
    # gem 'capistrano-rails', group: :development
    
    group :development, :test do
      # Call 'byebug' anywhere in the code to stop execution and get a debugger console
      gem 'byebug'
    end
    
    group :development do
      # Access an IRB console on exception pages or by using <%= console %> in views
      gem 'web-console', '~> 2.0'
    
      # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
      gem 'spring'
    end
    
    gem 'devise', '>= 4.0.0.rc1'
    gem 'sass', '~> 3.4', '>= 3.4.20'
    gem 'acts_as_votable', '~> 0.10.0'
    #comments/discussions:
    gem 'simple_form', '~> 3.2', '>= 3.2.1'
    gem 'puma'
    gem 'actioncable', '~> 5.0.0.rc1'
    gem 'therubyracer'
    gem 'execjs'
    gem 'record_tag_helper'
    gem 'redis' 

启动服务器后我的终端

Array values in the parameter to `Gem.paths=` are deprecated.
Please use a String or nil.
An Array ({"GEM_PATH"=>["/Users/Kegan/.rvm/gems/ruby-2.3.1", "/Users/Kegan/.rvm/gems/ruby-2.3.1@global"]}) was passed in from bin/rails:3:in `load'
=> Booting Puma
=> Rails 5.0.0.rc1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
[Simple Form] Simple Form is not configured in the application and will use the default values. Use `rails generate simple_form:install` to generate the Simple Form configuration.
Puma starting in single mode...
* Version 3.4.0 (ruby 2.3.1-p112), codename:
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

[回应格雷厄姆·斯利克]

App.room.speak('这有效吗?') 正确

————————————————————————

这有效吗?

————————————————————————

添加代码:(Javascripts/channels/room.coffee)

  received: (data) ->
    console.log(data)
    $('#messages').append "<div>#{data}</div>"

最佳答案

您没有任何带有 #messages ID 的 div,因此:

$('#messages').append "<div>#{data}</div>"

不会工作。 将其添加到 view/rooms/show.html.erb 中的 html 中:

 <h1>ActionCable chat</h1>
 <div id="messages">
   <%= render @messages %>
 </div>

另外,你的jquery不正确,它应该是:

 $('#messages').append("<div>" + data + "</div>")

关于javascript - Ruby on Rails、ActionCable、消息不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37404355/

相关文章:

javascript - 动态更新 MapView.UrlTile 组件的 urlTemplate 属性

javascript - 滑轨方式鼠标悬停或悬停,然后鼠标移回 post.image

ruby-on-rails - 用于 strip 化非字母和非数字字符的正则表达式

javascript - 是否可以在 ruby​​ 代码中使用 JS 代码?

ruby-on-rails - 雷迪斯 : sum aggregation for keys having some regex

mysql - 使用 mysql 查询数据更新 "Number"小部件 - Dashing

ruby - 搜索/替换怪异

javascript - 无法从外部 javascript 文件调用函数

ruby-on-rails - 包含 redirect_to 的测试因 rspec 而失败,但应用程序按预期工作。 Michael Hartl 书的第 9.6.6 章 (v 4.0)

javascript - 通过 AJAX 获取页面 HTML 的一部分