PrivatePub没有定义错误的Rails 3.2的私人酒吧(PrivatePub is not

2019-08-17 20:03发布

我想在我的Rails应用程序使用私人酒吧的宝石。 我railscast 316执行

rails g private_pub:install

米private_pub.yml文件:

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

我private_pub.ru文件

    # 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 }

我的索引文件

<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文件:

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

我添加//= require private_pub到的application.js文件

该页面被初始化后,我得到下面的萤火虫的错误:

**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> 

和在导轨没有输出日志和RAILS_ENV=production bundle exec rackup private_pub.ru -s thin -E production命令日志

我搜索,没有找到一个解决方案。

问题出在哪儿?

Answer 1:

我发现这个问题,我会写解决方案,因为任何人都应该得到这个错误:

我的JavaScript被放置在底部,

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

所以,在索引文件

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

前人的精力解决这个问题。



Answer 2:

我想你忘了添加private_pub.jsapplication.js

//= require private_pub


文章来源: PrivatePub is not defined error Rails 3.2 private pub