Rails 5.1 - ActionCable 没有更新?
Rails 5.1 - ActionCable not updating?
当我尝试 post 使用 ActionCable 发表评论时,一切正常,但是,我总是必须刷新页面才能看到评论本身得到呈现。任何想法怎么了?尝试重新启动服务器、注销、登录等。但没有任何帮助。
blogs.coffee
jQuery(document).on 'turbolinks:load', ->
comments = $('#comments')
if comments.length > 0
App.global_chat = App.cable.subscriptions.create {
channel: "BlogsChannel"
blog_id: comments.data('blog-id')
},
connected: ->
disconnected: ->
recieved: (data) ->
comments.append data['comment']
send_comment: (comment, blog_id) ->
@perform 'send_comment', comment: comment, blog_id: blog_id
$('#new_comment').submit (e) ->
$this = $(this)
textarea = $this.find('#comment_content')
if $.trim(textarea.val()).length > 1
App.global_chat.send_comment textarea.val(),
comments.data('blog-id')
textarea.val('')
e.preventDefault()
return false
Blogs_channel.rb
class BlogsChannel < ApplicationCable::Channel
def subscribed
stream_from "blogs_#{params['blog_id']}_channel"
end
def unsubscribed
end
def send_comment(data)
current_user.comments.create!(content: data['comment'], blog_id: data['blog_id'])
end
end
Comment.rb
class Comment < ApplicationRecord
belongs_to :user
belongs_to :blog
validates :content, presence: true, length: { minimum: 1, maximimum: 1000 }
after_create_commit { CommentBroadcastJob.perform_later(self) }
end
CommentBroadcastJob.rb
class CommentBroadcastJob < ApplicationJob
queue_as :default
def perform(comment)
ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
end
private
def render_comment(comment)
CommentsController.render partial: 'comments/comment', locals: { comment: comment }
end
end
_comment.html.erb
<div class="comment-card">
<div class="card">
<div class="card-block">
<div class="row">
<div class="col-md-1">
</div>
<div class="col-md-11">
<%= comment.content %>
</div>
</div>
</div>
</div>
</div>
评论在 show.html.erb 页面内呈现:
<%= render 'comments/comment_form' %>
<div id="comments" data-blog-id="<%= @blog.id %>">
<%= render @blog.comments %>
</div>
提交新评论时的控制台日志:
[ActionCable] [test1@test.com] [1] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2017-12-21 09:42:47 +0100
[ActionCable] [test1@test.com] [1] BlogsChannel stopped streaming from blogs_3_channel
Started GET "/cable" for 127.0.0.1 at 2017-12-21 09:42:48 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-12-21 09:42:48 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
[ActionCable] [test1@test.com] [1] Registered connection (Z2lkOi8vdWRlbXktdHV0b3JpYWwtcmFpbHMvVXNlci8x)
[ActionCable] [test1@test.com] [1] BlogsChannel is transmitting the subscription confirmation
[ActionCable] [test1@test.com] [1] BlogsChannel is streaming from blogs_3_channel
chrome 控制台或其他任何地方都没有错误。
有什么想法吗?在 route.rb 中,如果可能有帮助,我使用 mount ActionCable.server => '/cable'
。似乎它没有被激活/使用,不知道为什么。
提前致谢!
编辑:
有时,我在终端日志中收到以下错误:
[ActiveJob] [CommentBroadcastJob] [b32437ee-cf29-4323-97e9-841c87300518] Error performing CommentBroadcastJob (Job ID: b32437ee-cf29-4323-97e9-841c87300518) from Async(default) in 82.51ms: ActionView::Template::Error (undefined local variable or method `comment' for #<#<Class:0x00007f9b948bbed8>:0x00007f9b93a8fb98>
Did you mean? ��comment):
运行 byebug under def perform, result:
[2, 11] in
/app/jobs/comment_broadcast_job.rb
2: class CommentBroadcastJob < ApplicationJob
3: queue_as :default
4:
5: def perform(comment)
6: byebug
=> 7: ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
8: end
嗯,为什么评论未定义?同样,如果我刷新页面,即使有这个错误,我也会看到评论被渲染。
所以我从原始文件(来自一门课程)复制了 BroadCastJob.rb 文件,它终于可以工作了。我在我的文件和教师文件中找不到任何拼写错误。如果有人能找到它,请告诉我。
我的 CommentBroadcastJob.rb 文件:
class CommentBroadcastJob < ApplicationJob
queue_as :default
def perform(comment)
ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
end
private
def render_comment(comment)
CommentsController.render partial: 'comments/comment', locals: { comment: comment }
end
end
导师一:
class CommentBroadcastJob < ApplicationJob
queue_as :default
def perform(comment)
ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
end
private
def render_comment(comment)
CommentsController.render partial: 'comments/comment', locals: { comment: comment }
end
end
这是有效的。每当我删除这个并使用我的那个时,我都会遇到与以前相同的错误。当我使用讲师的时,它最终会在发送消息后呈现消息。不知道差异应该在哪里。
当我尝试 post 使用 ActionCable 发表评论时,一切正常,但是,我总是必须刷新页面才能看到评论本身得到呈现。任何想法怎么了?尝试重新启动服务器、注销、登录等。但没有任何帮助。
blogs.coffee
jQuery(document).on 'turbolinks:load', ->
comments = $('#comments')
if comments.length > 0
App.global_chat = App.cable.subscriptions.create {
channel: "BlogsChannel"
blog_id: comments.data('blog-id')
},
connected: ->
disconnected: ->
recieved: (data) ->
comments.append data['comment']
send_comment: (comment, blog_id) ->
@perform 'send_comment', comment: comment, blog_id: blog_id
$('#new_comment').submit (e) ->
$this = $(this)
textarea = $this.find('#comment_content')
if $.trim(textarea.val()).length > 1
App.global_chat.send_comment textarea.val(),
comments.data('blog-id')
textarea.val('')
e.preventDefault()
return false
Blogs_channel.rb
class BlogsChannel < ApplicationCable::Channel
def subscribed
stream_from "blogs_#{params['blog_id']}_channel"
end
def unsubscribed
end
def send_comment(data)
current_user.comments.create!(content: data['comment'], blog_id: data['blog_id'])
end
end
Comment.rb
class Comment < ApplicationRecord
belongs_to :user
belongs_to :blog
validates :content, presence: true, length: { minimum: 1, maximimum: 1000 }
after_create_commit { CommentBroadcastJob.perform_later(self) }
end
CommentBroadcastJob.rb
class CommentBroadcastJob < ApplicationJob
queue_as :default
def perform(comment)
ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
end
private
def render_comment(comment)
CommentsController.render partial: 'comments/comment', locals: { comment: comment }
end
end
_comment.html.erb
<div class="comment-card">
<div class="card">
<div class="card-block">
<div class="row">
<div class="col-md-1">
</div>
<div class="col-md-11">
<%= comment.content %>
</div>
</div>
</div>
</div>
</div>
评论在 show.html.erb 页面内呈现:
<%= render 'comments/comment_form' %>
<div id="comments" data-blog-id="<%= @blog.id %>">
<%= render @blog.comments %>
</div>
提交新评论时的控制台日志:
[ActionCable] [test1@test.com] [1] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2017-12-21 09:42:47 +0100
[ActionCable] [test1@test.com] [1] BlogsChannel stopped streaming from blogs_3_channel
Started GET "/cable" for 127.0.0.1 at 2017-12-21 09:42:48 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-12-21 09:42:48 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
[ActionCable] [test1@test.com] [1] Registered connection (Z2lkOi8vdWRlbXktdHV0b3JpYWwtcmFpbHMvVXNlci8x)
[ActionCable] [test1@test.com] [1] BlogsChannel is transmitting the subscription confirmation
[ActionCable] [test1@test.com] [1] BlogsChannel is streaming from blogs_3_channel
chrome 控制台或其他任何地方都没有错误。
有什么想法吗?在 route.rb 中,如果可能有帮助,我使用 mount ActionCable.server => '/cable'
。似乎它没有被激活/使用,不知道为什么。
提前致谢!
编辑:
有时,我在终端日志中收到以下错误:
[ActiveJob] [CommentBroadcastJob] [b32437ee-cf29-4323-97e9-841c87300518] Error performing CommentBroadcastJob (Job ID: b32437ee-cf29-4323-97e9-841c87300518) from Async(default) in 82.51ms: ActionView::Template::Error (undefined local variable or method `comment' for #<#<Class:0x00007f9b948bbed8>:0x00007f9b93a8fb98>
Did you mean? ��comment):
运行 byebug under def perform, result:
[2, 11] in
/app/jobs/comment_broadcast_job.rb
2: class CommentBroadcastJob < ApplicationJob
3: queue_as :default
4:
5: def perform(comment)
6: byebug
=> 7: ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
8: end
嗯,为什么评论未定义?同样,如果我刷新页面,即使有这个错误,我也会看到评论被渲染。
所以我从原始文件(来自一门课程)复制了 BroadCastJob.rb 文件,它终于可以工作了。我在我的文件和教师文件中找不到任何拼写错误。如果有人能找到它,请告诉我。
我的 CommentBroadcastJob.rb 文件:
class CommentBroadcastJob < ApplicationJob
queue_as :default
def perform(comment)
ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
end
private
def render_comment(comment)
CommentsController.render partial: 'comments/comment', locals: { comment: comment }
end
end
导师一:
class CommentBroadcastJob < ApplicationJob
queue_as :default
def perform(comment)
ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
end
private
def render_comment(comment)
CommentsController.render partial: 'comments/comment', locals: { comment: comment }
end
end
这是有效的。每当我删除这个并使用我的那个时,我都会遇到与以前相同的错误。当我使用讲师的时,它最终会在发送消息后呈现消息。不知道差异应该在哪里。