5

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_pubapplication.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コマンド ログに出力がありません

私は検索しましたが、解決策は見つかりませんでした。

問題はどこだ?

4

2 に答える 2

6

私は問題を見つけました。他の誰かがこのエラーを受け取るはずなので、解決策を書きます:

私のJavaScriptは一番下に配置されているので

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

だから、インデックスファイルで

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

これを解決する必要があります。

于 2013-03-21T09:59:00.780 に答える
0

private_pub.jsに追加するのを忘れたと思いますapplication.js

//= require private_pub
于 2015-10-08T06:58:28.520 に答える