在模型上使用Faye从数据库更改(after_commit回调)更新Rails视图

Yur*_*uri 9 javascript publish-subscribe faye ruby-on-rails-4

我很困惑,这是我的第一个Faye或Pub/Sub实现,如果这是一个基本问题,请原谅我.我还没有找到其他地方的答案.任何帮助表示赞赏.

如何从模型回调(after_commit,after_save等)调用和更新Rails视图页面?我需要一个javascript代码,一旦after_commit回调触发,视图更新将从views/meetings/_show_current_participants.js.erb运行.

如果我return => true在视图上使用link_to标记,我有它设置并且它可以使javascript执行并查看更新.问题是我将不会有任何用户交互,并且需要仅根据数据库更改将视图更新推送到页面基础(因此在模型上进行after_commit回调).这不是正确的工具/设计模式/方法吗?

我尝试在after_commit回调中对http:// localhost:3000/meetings/conference_id/_show_current_participants_url发出HTTP get请求,但是没有触发Faye/javascript在页面视图上执行.

这是:_show_current_participants.js.erb

<% participant_broadcast "/conferences/#{@conference.id}" do %>
  alert('_show_current_participants.js.erb loaded');
  $("#current_participants_js").html("<%= escape_javascript(render partial: 'conferences/show_current_participants.html', locals: { conference: @conference } ) %>");
<%end%>
Run Code Online (Sandbox Code Playgroud)

如果我将它设置为在具有remote => true的link_to标记上触发,这就像一个魅力:

<li><%= link_to "Faye CALL", "#{@conference.id}/_show_current_participants", remote: true %></li>
Run Code Online (Sandbox Code Playgroud)

如何修改此设置,而不必点击链接,在after_commit模型回调中激活Faye和后续部分的javascript?这是错误的方法吗?如何根据数据库更改告诉我的视图更新?任何帮助表示赞赏.非常感谢能够帮助我找到正确方向的善良灵魂.

版本:

Faye 1.2.4(不是faye-rails)
Rails 4.1.5
Ruby 2.3.0
此时不仅仅是一个非常漂亮的解决方案,我正在寻找有用的东西.

mic*_*ico 0

您应该使用 javascript 回调订阅消息:

<script>
   $(function() {
       // Create a new client to connect to Faye
       var client = new Faye.Client('http://localhost:3000/conferences');

       // Subscribe to the public channel
       var public_subscription = client.subscribe('/#{@conference.id}/_show_current_participants', function(data) {
          $("#current_participants_js").html(data);
        });
   });
</script>
Run Code Online (Sandbox Code Playgroud)

我的参考资料是从中获取和调整这段代码的:

https://code.tutsplus.com/tutorials/how-to-use-faye-as-a-real-time-push-server-in-rails--net-22600