我有文章模型,当用户登录时,或者当使用private_pub gem 发布新文章时,我想在主页上向已经登录的用户显示一条通知,比如一条闪光消息
articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.all
@articles_grid = initialize_grid(Article,
:include => [:user])
end
def show
@article = Article.find(params[:id])
@comment = Comment.new
end
def new
@article = Article.new
end
def create
@article = Article.new(params[:article])
@article.user_id = current_user.id
if @article.save
redirect_to articles_url, notice: 'Created article.'
PrivatePub.publish_to('articles/new', article: @article)
else
render :new
end
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
redirect_to articles_url, notice: 'Updated article.'
else
render :edit
end
end
end
articles.js.coffee
PrivatePub.subscribe 'articles/new',(data, channel) ->
alert data.article.content
在我的static_pages/home.html.erb 中
<%= subscribe_to '/articles/new' %>
当我创建一篇新文章时,它成功地创建了,但什么都没有发生,没有通知
您的application.js中包含//= require private_pub
了吗?
你有法耶跑步吗?rackup private_pub.ru -s thin -E production
假设您已按照https://github.com/ryanb/private_pub我调试的方法是在Google Chrome控制台中打开应用程序,查看底部的网络选项卡和websocket,看看是否建立了连接。之后,我会启动rails console
并发送一些PrivatePub.publish_to('articles/new', article: 'hello world')
,看看它们是否会出现在Chrome 中