我有文章模型,我希望在用户登录时向主页显示主页上的flash消息,或者在使用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' %>
当我创建新文章时,它已成功创建,但没有任何事情发生通知
答案 0 :(得分:1)
您是否已将//= require private_pub
包含在application.js中?
你有跑王吗? 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中