gpt4 book ai didi

jquery - 如何使用 jQuery 将值传递到 coffescript ActionCable

转载 作者:行者123 更新时间:2023-12-01 08:42:08 24 4
gpt4 key购买 nike

我正在尝试创建动态 channel 供用户聊天,但无法将 room_id 传递到 App.cable.subscriptions.create 调用中在 room.coffee 文件中。任何有助于澄清为什么我不能使用 jQuery 获取该值的帮助都非常有帮助。

这是我在控制台中收到的内容:

RoomChannel is transmitting the subscription confirmation
RoomChannel is streaming from room__channel

房间 Controller

class RoomsController < ApplicationController
before_action :authenticate_user!

def show
other_user = User.find(params[:id])
title = [other_user.id.to_s, current_user.id.to_s].sort!.join("-")
@room = Room.where(title: title).first
if @room.nil?
@room = Room.create! title: title
else
@messages = Message.where(room_id: @room[:id])
end
end

end

show.html.erb

<h1>Chat Room</h1>
<div id="messages">
<% if @messages.blank? %>
<div id="no_messages_yet">
<p>No messages yet...</p>
</div>
<% else %>
<%= render @messages %>
<% end %>
</div>
<form>
<label>Say something:</label><br>
<input type="text" data-behavior="room_speaker">
<input type="hidden" value="<%= @room[:id] %>" id="room_id">
<input type="hidden" value="<%= current_user[:id] %>" id="user_id">
</form>

views/messages/_message.html.erb

<div class="message">
<p><%= message.content %></p>
</div>

房间.咖啡

$(document).ready ->
room_id = $("#room_id").val()
console.log(room_id)
// => 1
// When I try and pass 'room_id' as the value for room_id:, I get an error stating the variable is not recognized.
App.room = App.cable.subscriptions.create {channel: "RoomChannel", room_id: $('#room_id').val()},
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) ->
$('#messages').append data['message']
# Called when there's incoming data on the websocket for this channel

speak: (message, room_id, user_id) ->
@perform 'speak', message: message, room_id: room_id, user_id: user_id

$(document).on 'keypress', '[data-behavior~=room_speaker]', (event) ->
if event.keyCode is 13 # return = send
App.room.speak event.target.value, $('#room_id').val(), $('#user_id').val()
event.target.value = ""
event.preventDefault()

房间 channel

class RoomChannel < ApplicationCable::Channel
def subscribed
stream_from "room_#{params[:room_id]}_channel"
end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end

def speak(data)
m = Message.new
m.user_id = data["user_id"]
m.room_id = data["room_id"]
m.content = data["message"]
m.save!
end
end

消息.rb

class Message < ApplicationRecord
after_create_commit { MessageBroadcastJob.perform_later self }
end

jobs/message_broadcast_job.rb

class MessageBroadcastJob < ApplicationJob
queue_as :default

def perform(message)
ActionCable.server.broadcast "room_#{message.room_id}_channel", message: render_message(message)
end

private
def render_message(message)
ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })
end
end

最佳答案

将功能放入 $(document).ready 调用中。请记住,coffeescript 在缩进和空格方面非常敏感。

$(document).ready ->
room_id = $('#room_id').val()
App.room = App.cable.subscriptions.create {channel: "RoomChannel", room_id: room_id},

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) ->
$('#messages').append data['message']
# Called when there's incoming data on the websocket for this channel

speak: (message, room_id, user_id) ->
@perform 'speak', message: message, room_id: room_id, user_id: user_id

$(document).on 'keypress', '[data-behavior~=room_speaker]', (event) ->
if event.keyCode is 13 # return = send
App.room.speak event.target.value, $('#room_id').val(), $('#user_id').val()
event.target.value = ""
event.preventDefault()

关于jquery - 如何使用 jQuery 将值传递到 coffescript ActionCable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45686249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com