PrivatePub is not defined error Rails 3.2 private

2019-04-09 07:05发布

I want to use private pub gem in my rails application. I implement with railscast 316

After rails g private_pub:install

my private_pub.yml file:

development:
  server: "http://0.0.0.0:9292/faye"
  secret_token: "secret"
test:
  server: "http://0.0.0.0:9292/faye"
  secret_token: "secret"
production:
  server: "http://0.0.0.0/faye"
  secret_token: "98ec77eb7077c9899dc53f003abc4d6a0170512a57feab126ed5a32b114e3613"
  signature_expiration: 3600 # one hour

my private_pub.ru file

    # Run with: rackup private_pub.ru -s thin -E production
    require "bundler/setup"
    require "yaml"
    require "faye"
    require "private_pub"

    Faye::WebSocket.load_adapter('thin')

    PrivatePub.load_config(File.expand_path("../config/private_pub.yml", __FILE__), ENV["RAILS_ENV"] || "development")
    run PrivatePub.faye_app

Faye::Logging.log_level = :debug
Faye.logger = lambda { |m| puts m }

my index file

<h1>Chat</h1>

<ul id="chat">
  <%= render @mesajlar %>
</ul>

<%= form_for Mesaj.new, remote: true do |f| %>
    <%= f.text_field :icerik %>
    <%= f.submit "Send" %>
<% end %>

<%= subscribe_to "/mesajlar/new" %>

Controller file:

def create
params[:mesaj][:gonderen_id]= current_kullanici.id
@mesaj = Mesaj.create!(params[:mesaj])
PrivatePub.publish_to("/mesajlar/new", "alert('#{@mesaj.icerik}');")
end 

I add //= require private_pub to application.js file

After the page was initialized I get the error below on firebug:

**PrivatePub is not defined**
at 
<script type="text/javascript">PrivatePub.sign({"server":"http://0.0.0.0:9292","timestamp":1363853952047,"channel":"/mesajlar/new","signature":"7bf74474796412b524b6c6c8849c50cb245ce92d"});</script>
</div> 

And no output in Rails log and RAILS_ENV=production bundle exec rackup private_pub.ru -s thin -E production commands log

I searched and no found a solution.

Where is the problem?

2条回答
闹够了就滚
2楼-- · 2019-04-09 07:28

I think you forgot to add private_pub.js to application.js:

//= require private_pub
查看更多
聊天终结者
3楼-- · 2019-04-09 07:37

I find the problem, I will write solution because anyone else should get this error:

My javascripts is placed in bottom so

<%= subscribe_to "/mesajlar/new" %> line calling before private_pub.js is loaded.

So, in index file

<% content_for :bottom do %>
<%= subscribe_to "/mesajlar/new" %>
<% end  %>

sould solve this.

查看更多
登录 后发表回答