如何在Rails 7中设置ActionCable?

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

How to setup actioncable in rails 7?

问题

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

  1. rails generate channel chat_room

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

# app/channels/chatroom_channel.rb
class ChatroomChannel < ApplicationCable::Channel
  def connected
    console.log("Connected to Actioncable")
  end

  def disconnected
    # 当客户端断开连接时的逻辑
  end

  def received(data)
    # 当客户端从通道接收数据时的逻辑
  end
end
  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 §
# app/channels/chatroom_channel.rb
class ChatroomChannel &lt; ApplicationCable::Channel
  def connected
    console.log(&quot;Connected to Actioncable&quot;)
  end

  def disconnected
    # Logic when a client disconnects from the channel
  end

  def received(data)
    # Logic when a client receives data from the channel
  end
end
  1. added this in the routes file :
  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

# 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
# app/channels/chat_channel.rb

class ChatChannel < ApplicationCable::Channel
  def subscribed
    stream_from "the_chat"
  end
end
# app/controllers/messages_controller.rb

def create
  @message = Message.create(message_params)
  ActionCable.server.broadcast("the_chat", @message)
  head :ok
end
// app/javascript/channels/chat_channel.js

import consumer from "channels/consumer";

consumer.subscriptions.create("ChatChannel", {
  connected() {
    console.log("connected")
  },
  received(data) {
    const messages = document.querySelector("#messages")
    messages.insertAdjacentHTML("beforeend", `<p>${data.content}</p>`)
    messages.lastElementChild.scrollIntoView({behavior: "smooth"})
  }
})
# app/views/messages/index.html.erb

<%= tag.div do %>
  <%= tag.div id: "messages", class: "h-96 overflow-auto" do %>
    <% Message.last(10).each do |message| %>
      <%= tag.p message.content %>
    <% end %>
  <% end %>

  <%= form_with model: Message.new do |f| %>
    <%= f.text_field :content, placeholder: "message..." %>
    <%= f.submit "send", class: "p-2 font-bold" %>
  <% end %>
<% end  %>

Also, like that:

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

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


<details>
<summary>英文:</summary>

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


```rb
# app/channels/chat_channel.rb

class ChatChannel &lt; ApplicationCable::Channel
  def subscribed
    stream_from &quot;the_chat&quot;
  end
end
# app/controllers/messages_controller.rb

def create
  @message = Message.create(message_params)
  ActionCable.server.broadcast(&quot;the_chat&quot;, @message)
  head :ok
end
// app/javascript/channels/chat_channel.js

import consumer from &quot;channels/consumer&quot;

consumer.subscriptions.create(&quot;ChatChannel&quot;, {
  connected() {
    console.log(&quot;connected&quot;)
  },
  received(data) {
    const messages = document.querySelector(&quot;#messages&quot;)
    messages.insertAdjacentHTML(&quot;beforeend&quot;, `&lt;p&gt;${data.content}&lt;/p&gt;`)
    messages.lastElementChild.scrollIntoView({behavior: &quot;smooth&quot;})
  }
})
# app/views/messages/index.html.erb

&lt;%= tag.div do %&gt;
  &lt;%= tag.div id: &quot;messages&quot;, class: &quot;h-96 overflow-auto&quot; do %&gt;
    &lt;% Message.last(10).each do |message| %&gt;
      &lt;%= tag.p message.content %&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;

  &lt;%= form_with model: Message.new do |f| %&gt;
    &lt;%= f.text_field :content, placeholder: &quot;message...&quot; %&gt;
    &lt;%= f.submit &quot;send&quot;, class: &quot;p-2 font-bold&quot; %&gt;
  &lt;% end %&gt;
&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:

确定