【问题标题】:PrivatePub is not defined error Rails 3.2 private pubPrivatePub 未定义错误 Rails 3.2 private pub
【发布时间】:2013-03-21 08:23:23
【问题描述】:

我想在我的 rails 应用程序中使用私有 pub gem。我用 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 

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

页面初始化后,我在 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 命令日志中没有输出

我搜索并没有找到解决方案。

问题出在哪里?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 faye private-pub


    【解决方案1】:

    我找到了问题,我会写解决方案,因为其他人应该会得到这个错误:

    我的javascripts放在底部所以

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

    所以, 在索引文件中

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

    应该可以解决这个问题。

    【讨论】:

      【解决方案2】:

      我想你忘了将private_pub.js 添加到application.js

      //= require private_pub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-03
        • 2012-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-01
        相关资源
        最近更新 更多