Ruby on rails 如何识别要发送数据的当前用户(ActiveCable)

Ruby on rails 如何识别要发送数据的当前用户(ActiveCable),ruby-on-rails,ruby,coffeescript,Ruby On Rails,Ruby,Coffeescript,我想知道是否可以使用当前用户的方法将数据发送到咖啡文件中 就我而言,我正在创建一些组。我做了两个容器来区分你的和其他的。所以我试图在接收函数中设置一个条件。但我不知道怎么做(您可以在下面我的代码中看到一个try) 因此,如果你有一个解决方案,这将是伟大的 我的代码: 视图(索引):带有两个容器 <div class="col-sm-12"> <div class="container"> <h1 style="text-align:center; ma

我想知道是否可以使用当前用户的方法将数据发送到咖啡文件中

就我而言,我正在创建一些组。我做了两个容器来区分你的和其他的。所以我试图在接收函数中设置一个条件。但我不知道怎么做(您可以在下面我的代码中看到一个try)

因此,如果你有一个解决方案,这将是伟大的

我的代码:

视图(索引):带有两个容器

<div class="col-sm-12"> 
  <div class="container">
    <h1 style="text-align:center; margin-bottom: 30px; margin-top: 10px;">Your groups</h1>
    <div id="groups_area">  
    </div>
  </div>

  <div class="container">
    <h1 style="text-align:center; margin-bottom: 30px; margin-top: 10px;">Other groups </h1>
    <div id="groups_out_area">     
    </div>
  </div>
</div>
我需要在ActionCable文件中捕捉它吗?正如您在这里看到的,我试图用speak方法捕捉它,但这并不是真正的逻辑

class GroupsChannel < ApplicationCable::Channel
  def subscribed
    stream_from "groups"
  end

  def speak(data)
    group = Group.create(name: data['group'], company_id: current_user.company_id, admin_id: current_user.id)
    user_group = GroupsUser.create(user_id: current_user.id, group_id: group.id)
    html = ApplicationController.render(partial: 'groups/group', locals: {
      group: group
    })

    ActionCable.server.broadcast 'groups', group: html
  end 

end
class-GroupsChannel
您可以将
当前用户.id
传递到视图,然后将其放入咖啡文件中。感谢@yzalavin的帮助,但您不知道如何将当前用户直接调用到咖啡脚本中?@yzalavin建议通过
coffee.erb
或类似
当前用户\u id=
之类的方式加载变量。请记住,您不能依赖客户端逻辑来安全地阻止用户查看某些内容;您需要确保服务器也只向这些用户发布。您可以将
current_user.id
传递到视图,然后将其保存到coffee文件中。感谢您的帮助@yzalavin,但是您不知道如何将当前用户直接调用到coffee脚本中?@yzalavin建议通过
coffee.erb
或类似
current\u user\u id=
之类的方式加载变量。请记住,您不能依赖客户端逻辑来安全地阻止用户查看某些内容;您需要确保服务器也只向这些用户发布。
class GroupsChannel < ApplicationCable::Channel
  def subscribed
    stream_from "groups"
  end

  def speak(data)
    group = Group.create(name: data['group'], company_id: current_user.company_id, admin_id: current_user.id)
    user_group = GroupsUser.create(user_id: current_user.id, group_id: group.id)
    html = ApplicationController.render(partial: 'groups/group', locals: {
      group: group
    })

    ActionCable.server.broadcast 'groups', group: html
  end 

end