我正在尝试创建一个“大厅”,您可以在其中加入聚会,另一个用户从列表中接受他想要的人,我正在使用 ActionCable 和 Ruby on Rails < / strong>
传入请求显示来自接收数据的最后一个用户的信息,并且必须向我们显示传入请求的信息(不要与其他用户数据一起更改视图!)。
>App.join = App.cable.subscriptions.create "JoinChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
$('#join_list_table').empty()
this.render_list(data)
$(".button-collapse").sideNav();
$(".modal-trigger").click ->
joinListId = $(this).attr('id')
App.join.joinTheList joinListId
# Called when there's incoming data on the websocket for this channel
joinTheList: (listId) ->
@perform "join", list_id: listId
$('.modal').modal opacity: 0
leave: ->
render_list: (data) ->
joinLists = data['joinLists']
for joinList in joinLists
joinings = joinList['joinings']
if joinList['user']['id'] != data['current_user_id'] # here must say if the joinlist have the same id than the user
$('#join_list_table').append(
"<tr>
<td>#{joinList['user']['name']}</td>
<td>#{joinList['user']['email']}</td>
<td>#{joinList['user']['language']}</td>
<td>#{joinList['user']['country_code']}</td>
<td>#{joinList['user']['price']}</td>
<td>#{joinList['user']['currency']}</td>
<td>
<a class='modal-trigger' id='#{joinList['list']['id']}' href='#modal#{joinList['list']['id']}' >
Join
</a>
</td>
</tr>
")
$('main').append(
"<div class='modal z-depth-0' id='modal#{joinList['list']['id']}'>
<div class='modal-content'>
<div class='col s12'>
<ul id='joinings#{joinList['list']['id']}' class='collection with-header'>
<li class='collection-header'><h4>Join list for #{joinList['user']['name']}</h4></li>
</ul>
</div>
</div>
</div>")
for joining in joinings
$("#user_#{joining['user']['id']}_joining").remove();
$("#joinings#{joinList['list']['id']}").append("<li id='user_#{joining['user']['id']}_joining'><a class='collection-item z-depth-0 center-align'>#{joining['user']['name']}</a></li>")
else
joinings = joinList['joinings']
for joining in joinings
$('#slide-out').append("<li id='user_#{joining['user']['id']}_joining'><a class='collection-item z-depth-0 center-align'>#{joining['user']['name']}</a></li>")
document.addEventListener 'turbolinks:load', ->
App.join.perform "render_all"
$('#query').on 'keyup', ->
value = $(this).val().toLowerCase()
$('tr').filter ->
$(this).toggle $(this).text().toLowerCase().indexOf(value) > -1
class JoinChannel < ApplicationCable::Channel
def subscribed
unless current_user.join_list
current_user.create_join_list
end
stream_from "join_channel"
render_all
end
def unsubscribed
if current_user.join_list.joinings
current_user.join_list.joinings.destroy_all
end
current_user.join_list.destroy
# Any cleanup needed when channel is unsubscribed
end
def join(data)
joinlist = JoinList.find(data['list_id'])
unless current_user.joining
puts "esta creando"
current_user.create_joining(join_list_id: joinlist.id)
else
puts "esta actualizando"
current_user.joining.update(join_list_id: joinlist.id)
end
render_all
end
def render_all
joinlistArr = []
joinlists = JoinList.joins(:user)
joinlists.each do |joinlist|
joiningusers = []
joinlist.joinings.each do |joining|
joiningusers << {'user' => joining.user}
end
joinlistArr << {'list' => joinlist, 'user' => joinlist.user, 'joinings' => joiningusers}
end
puts "dame los joinings de la joinlist del current_user"
puts current_user.join_list.joinings
ActionCable.server.broadcast "join_channel", {joinLists: joinlistArr, current_user_id: current_user.id}
end
end
答案 0 :(得分:0)
已解决!
更改
ActionCable.server.broadcast "join_channel", {joinLists: joinlistArr, current_user_id: current_user.id}
用于
JoinChannel.broadcast_to(
current_user,
joinLists: joinlistArr,
current_user_id: current_user.id
)