Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在RubyonRails中从视图访问pubnub的已发布属性_Ruby On Rails_Handlebars.js_Pubnub - Fatal编程技术网

Ruby on rails 如何在RubyonRails中从视图访问pubnub的已发布属性

Ruby on rails 如何在RubyonRails中从视图访问pubnub的已发布属性,ruby-on-rails,handlebars.js,pubnub,Ruby On Rails,Handlebars.js,Pubnub,在我的rails项目中,我使用pubnub来实时更新一些东西,并使用Handlebar作为实时更新模板。下面是发布更新的代码 subscriber_channels.each do |subscriber_channel| $pubnub_server_subscription.publish( :channel => subscriber_channel, :message => {event:

在我的rails项目中,我使用pubnub来实时更新一些东西,并使用Handlebar作为实时更新模板。下面是发布更新的代码

subscriber_channels.each do |subscriber_channel|
            $pubnub_server_subscription.publish(
                :channel => subscriber_channel,
                :message => {event: 'LIVE_FEED',
                             attributes: {
                                 live_feed_id: self.id,
                                 feedable_type: self.feedable_type,
                                 feedable_id: self.feedable_id,
                                 profile_picture_url: "#{self.action_user.profile_photo_thumb}",
                                 action_user_name: "#{self.action_user.full_name}",
                                 live_feed_content: "#{self.content}",
                                 feed_time: "#{self.created_at.strftime("%H:%M")}",
                                 reference_post_id: reference_post_id,
                                 reference_post_type: self.reference_post_type
                             }
                },
                callback: lambda { |info| puts info }
            )
          end
对于模板,我的代码如下

PUBNUB_CLIENT.events.bind("LIVE_FEED", function (message) {
var livefeedTemplate = Handlebars.compile($('#top-live-feed-template').html());
              var outputHtml = livefeedTemplate(message.attributes);
              $('#live-feed-table').prepend(outputHtml);
}
到目前为止,上述代码工作正常。但是在调用模板之前,我需要做一些检查。我想要这样的东西

    PUBNUB_CLIENT.events.bind("LIVE_FEED", function (message) {
if feedable_type == "image" #this feedable_type from published attributes above
        var livefeedTemplate = Handlebars.compile($('#top-live-feed-template').html());
                      var outputHtml = livefeedTemplate(message.attributes);
                      $('#live-feed-table').prepend(outputHtml);
        }
    }
feedable_type这里是上面发布的属性。那我该怎么做呢?或者我怎么能做这样的事

console.log( "update a <div> with received event: feedable_type");
console.log(“用接收到的事件更新a:feedable_type”);
试试这个:

PUBNUB_CLIENT.events.bind("LIVE_FEED", function (message) {
    if(message.attributes.feedable_type == "image"){
        var livefeedTemplate = Handlebars.compile($('#top-live-feed-template').html());
        var outputHtml = livefeedTemplate(message.attributes);
        $('#live-feed-table').prepend(outputHtml);
    }
}

如果这不起作用,请提供更多详细信息。

您使用的是PubNub Gem还是PubNub JavaScript SDK?