0

I have article model and i want to show a notification such as a flash message on the home page to the users when they log in or who is logged in already when a new article is posted using 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

In my static_pages/home.html.erb

<%= subscribe_to '/articles/new' %>

when I create a new article it created successfully but nothing happen no notification

Mostafa Hussein
  • 11,063
  • 3
  • 36
  • 61

1 Answers1

1

Have you included //= require private_pub in your application.js?

Do you have have faye running? rackup private_pub.ru -s thin -E production

Assuming that you have followed the instructions at https://github.com/ryanb/private_pub the way I would debug this is open the application in Google Chrome console and look at the network tab and websocket at the bottom to see if a connection was established. After that I would launch rails console and send a few PrivatePub.publish_to('articles/new', article: 'hello world') to see if they would come up in Chrome

user1894919
  • 148
  • 7
  • The network tab and websocket do not show "faye" but the rackup command seems to launch without any problems, i am confused – Anil Muppalla Dec 25 '13 at 14:25
  • I've got the same problem. When I checked the google chrome console. It appears to be searching for 'server value in private_pub.yml' + .js file. Don't know why its appending .js to it – Akshay Takkar Dec 28 '13 at 14:52
  • Particulary, I have moved away from PrivatePub and decided to use SockJS and integrated my own nodejs websocket server with Rails (The PrivatePub wasn't working properly with iOS). If you are still using PrivatePub though, it is not supported by the developer anymore. An alternative would a fork of PrivatePub called d'Anthès https://github.com/dotpromo/danthes – user1894919 Sep 02 '14 at 05:28