PrivatePub未定义错误 Rails 3.2 私有发布

5

我希望在我的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" %>

控制器文件:

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

我在application.js文件中添加了//= require private_pub

页面初始化后,我在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> 

在 Rails 日志和 RAILS_ENV=production bundle exec rackup private_pub.ru -s thin -E production 命令日志中都没有输出。

我搜索了但未找到解决方法。

问题出在哪里?

2个回答

6
我发现问题所在,我会写下解决方案,因为其他人可能也会遇到这个错误:
我的JavaScript代码放在底部,所以...
<%= subscribe_to "/mesajlar/new" %> line calling before private_pub.js is loaded.

所以,在索引文件中。
<% content_for :bottom do %>
<%= subscribe_to "/mesajlar/new" %>
<% end  %>

应该解决这个问题。


0

我认为您忘记将private_pub.js添加到application.js中:

//= require private_pub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接