如何在Rails 7中设置ActionCable?

huangapple go评论95阅读模式
英文:

How to setup actioncable in rails 7?

问题

我已经尝试了几天,但一直无法获得Rails 7应用程序。我尝试了许多教程,其中许多已经过时,没有得到有用的结果。我也尝试了很长时间的ChatGPT,但也没有成功。我已经采取的步骤如下:

  1. rails generate channel chat_room

  2. 在app/channels/chatroom_channel.rb文件中添加以下内容:

  1. # app/channels/chatroom_channel.rb
  2. class ChatroomChannel < ApplicationCable::Channel
  3. def connected
  4. console.log("Connected to Actioncable")
  5. end
  6. def disconnected
  7. # 当客户端断开连接时的逻辑
  8. end
  9. def received(data)
  10. # 当客户端从通道接收数据时的逻辑
  11. end
  12. end
  1. 在路由文件中添加以下内容:
  1. mount ActionCable.server => "/cable"
  1. 启动Rails服务器。

  2. 查看JavaScript控制台是否显示消息 "connected to actioncable"。

  3. 我没有看到消息。

英文:

I have been trying to get Rails 7 application for days to no avail. I have tried many tutorials, many of which have been outdated, and gotten no useful results. I tried ChatGPT for quite a while to no avail as well. steps I have taken:

  1. rails generate channel chat_room
  2. have this in the app/channels/chatroom_channel.rb file §
  1. # app/channels/chatroom_channel.rb
  2. class ChatroomChannel &lt; ApplicationCable::Channel
  3. def connected
  4. console.log(&quot;Connected to Actioncable&quot;)
  5. end
  6. def disconnected
  7. # Logic when a client disconnects from the channel
  8. end
  9. def received(data)
  10. # Logic when a client receives data from the channel
  11. end
  12. end
  1. added this in the routes file :
  1. mount ActionCable.server =&gt; &quot;/cable&quot;
  1. started rails server.

  2. looked at the javascript console to the message "connected to
    actioncable"

6.I don't see a message

答案1

得分: 1

  1. # Rails 7.0.6
  2. rails new hello_cable -c tailwind
  3. cd hello_cable
  4. bin/rails g channel chat
  5. bin/rails g scaffold Message content
  6. bin/rails db:migrate
  7. open http://localhost:3000/messages
  8. bin/dev
  1. # app/channels/chat_channel.rb
  2. class ChatChannel < ApplicationCable::Channel
  3. def subscribed
  4. stream_from "the_chat"
  5. end
  6. end
  1. # app/controllers/messages_controller.rb
  2. def create
  3. @message = Message.create(message_params)
  4. ActionCable.server.broadcast("the_chat", @message)
  5. head :ok
  6. end
  1. // app/javascript/channels/chat_channel.js
  2. import consumer from "channels/consumer";
  3. consumer.subscriptions.create("ChatChannel", {
  4. connected() {
  5. console.log("connected")
  6. },
  7. received(data) {
  8. const messages = document.querySelector("#messages")
  9. messages.insertAdjacentHTML("beforeend", `<p>${data.content}</p>`)
  10. messages.lastElementChild.scrollIntoView({behavior: "smooth"})
  11. }
  12. })
  1. # app/views/messages/index.html.erb
  2. <%= tag.div do %>
  3. <%= tag.div id: "messages", class: "h-96 overflow-auto" do %>
  4. <% Message.last(10).each do |message| %>
  5. <%= tag.p message.content %>
  6. <% end %>
  7. <% end %>
  8. <%= form_with model: Message.new do |f| %>
  9. <%= f.text_field :content, placeholder: "message..." %>
  10. <%= f.submit "send", class: "p-2 font-bold" %>
  11. <% end %>
  12. <% end %>

Also, like that:

https://stackoverflow.com/a/75728527/207090

https://stackoverflow.com/a/75808949/207090

  1. <details>
  2. <summary>英文:</summary>
  3. Like this:

Rails 7.0.6

rails new hello_cable -c tailwind
cd hello_cable
bin/rails g channel chat
bin/rails g scaffold Message content
bin/rails db:migrate
open http://localhost:3000/messages
bin/dev

  1. ```rb
  2. # app/channels/chat_channel.rb
  3. class ChatChannel &lt; ApplicationCable::Channel
  4. def subscribed
  5. stream_from &quot;the_chat&quot;
  6. end
  7. end
  1. # app/controllers/messages_controller.rb
  2. def create
  3. @message = Message.create(message_params)
  4. ActionCable.server.broadcast(&quot;the_chat&quot;, @message)
  5. head :ok
  6. end
  1. // app/javascript/channels/chat_channel.js
  2. import consumer from &quot;channels/consumer&quot;
  3. consumer.subscriptions.create(&quot;ChatChannel&quot;, {
  4. connected() {
  5. console.log(&quot;connected&quot;)
  6. },
  7. received(data) {
  8. const messages = document.querySelector(&quot;#messages&quot;)
  9. messages.insertAdjacentHTML(&quot;beforeend&quot;, `&lt;p&gt;${data.content}&lt;/p&gt;`)
  10. messages.lastElementChild.scrollIntoView({behavior: &quot;smooth&quot;})
  11. }
  12. })
  1. # app/views/messages/index.html.erb
  2. &lt;%= tag.div do %&gt;
  3. &lt;%= tag.div id: &quot;messages&quot;, class: &quot;h-96 overflow-auto&quot; do %&gt;
  4. &lt;% Message.last(10).each do |message| %&gt;
  5. &lt;%= tag.p message.content %&gt;
  6. &lt;% end %&gt;
  7. &lt;% end %&gt;
  8. &lt;%= form_with model: Message.new do |f| %&gt;
  9. &lt;%= f.text_field :content, placeholder: &quot;message...&quot; %&gt;
  10. &lt;%= f.submit &quot;send&quot;, class: &quot;p-2 font-bold&quot; %&gt;
  11. &lt;% end %&gt;
  12. &lt;% end %&gt;

Also, like that:

https://stackoverflow.com/a/75728527/207090

https://stackoverflow.com/a/75808949/207090

huangapple
  • 本文由 发表于 2023年7月27日 21:45:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定