Ruby on rails 有没有办法向action cable聊天室添加临时用户名

Ruby on rails 有没有办法向action cable聊天室添加临时用户名,ruby-on-rails,ruby,actioncable,livechat,Ruby On Rails,Ruby,Actioncable,Livechat,您好,我在action cable上设置了一个基本的聊天室,我想知道在聊天室中发帖时是否有办法为当前用户提供一个临时用户名?最好设置为在允许他们聊天之前必须输入用户名 提前谢谢 下面是我的代码: 这是一种观点: <h1>Chat room</h1> <div id="messages"> <%= render @messages %> </div> <form> <label>Say something:

您好,我在action cable上设置了一个基本的聊天室,我想知道在聊天室中发帖时是否有办法为当前用户提供一个临时用户名?最好设置为在允许他们聊天之前必须输入用户名

提前谢谢

下面是我的代码:

这是一种观点:

<h1>Chat room</h1>

<div id="messages">
 <%= render @messages %>
</div>

<form>
 <label>Say something:</label><br>
 <input type="text" data-behavior="room_speaker">

</form>
这是我的控制器:

 class RoomsController < ApplicationController
 def index
  @messages = Message.all
 end

end
class RoomsController
我呈现的页面:

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

解决方案1 最初将用户重定向到页面,在该页面上有文本框输入用户名 用户输入用户名并单击提交后,将其重定向到聊天页面。 现在您有了params[:user_name],只需在聊天操作中创建@username,并将其作为聊天表单中的隐藏变量。(即用户输入聊天信息的形式)

现在您的表单将如下所示

class RoomsController < ApplicationController

 #index action will render form with just text_field to enter username and submit

 def index
 end

 # Now chat_window will handle you chat_form
 # From index action you will receive params username. Just create object with it
 # so that you can refer it in chat form
 def chat_window 
  @username = params[:user_name] 
  @messages = Message.all
 end
end
def speak(data, user_name)
  # here you got user_name.Do whatever.
  # Either create field in message model called user_name or Separate User model.
  Message.create! content: data['message']
end
在你的房间频道会有这样的用户名

class RoomsController < ApplicationController

 #index action will render form with just text_field to enter username and submit

 def index
 end

 # Now chat_window will handle you chat_form
 # From index action you will receive params username. Just create object with it
 # so that you can refer it in chat form
 def chat_window 
  @username = params[:user_name] 
  @messages = Message.all
 end
end
def speak(data, user_name)
  # here you got user_name.Do whatever.
  # Either create field in message model called user_name or Separate User model.
  Message.create! content: data['message']
end
解决方案1 最初将用户重定向到页面,在该页面上有文本框输入用户名 用户输入用户名并单击提交后,将其重定向到聊天页面。 现在您有了params[:user_name],只需在聊天操作中创建@username,并将其作为聊天表单中的隐藏变量。(即用户输入聊天信息的形式)

现在您的表单将如下所示

class RoomsController < ApplicationController

 #index action will render form with just text_field to enter username and submit

 def index
 end

 # Now chat_window will handle you chat_form
 # From index action you will receive params username. Just create object with it
 # so that you can refer it in chat form
 def chat_window 
  @username = params[:user_name] 
  @messages = Message.all
 end
end
def speak(data, user_name)
  # here you got user_name.Do whatever.
  # Either create field in message model called user_name or Separate User model.
  Message.create! content: data['message']
end
在你的房间频道会有这样的用户名

class RoomsController < ApplicationController

 #index action will render form with just text_field to enter username and submit

 def index
 end

 # Now chat_window will handle you chat_form
 # From index action you will receive params username. Just create object with it
 # so that you can refer it in chat form
 def chat_window 
  @username = params[:user_name] 
  @messages = Message.all
 end
end
def speak(data, user_name)
  # here you got user_name.Do whatever.
  # Either create field in message model called user_name or Separate User model.
  Message.create! content: data['message']
end

谢谢你的回复,当你说在聊天中创建@username时,你的真正意思是,我将参数发送到哪里?对不起,我是新来的this@dave你有你的聊天窗体所在的控制器,对吗?你能出示密码吗it@dave我更新代码。检查我的解决方案的结尾,在那里我更新了just form。@dave我刚刚更新了最新的代码,以供您理解。如果有,一定要告诉我doubt@dave谢谢你的回复,当你说在聊天活动中创建@username时,你的真正意思是,我将参数发送到哪里?对不起,我是新来的this@dave你有你的聊天窗体所在的控制器,对吗?你能出示密码吗it@dave我更新代码。检查我的解决方案的结尾,在那里我更新了just form。@dave我刚刚更新了最新的代码,以供您理解。如果有,一定要告诉我doubt@dave谢谢你,兄弟