У меня проблемы с моим Action Cable, я использую приложение rails с javascript спереди, мой фронт подключен к action action, но метод received
никогда не вызывается, но connected
- это вызов, и я получаю ping
из ActionCable.
Это мой код JavaScript:
categories.js
$(document).ready(function () {
App.category = App.cable.subscriptions.create("CategoryChannel",
{
received: function(data) {
console.log(data);
alert("Category is done!");
},
disconnected: function() {
console.log("We are not connected");
},
connected: function () {
console.log("We are connected");
}
}
);
});
cable.js
// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
//
//= require action_cable
//= require_self
//= require_tree ./channels
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);
Это мой канал:
category_channel.rb
class CategoryChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
stream_for 'categories'
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
И мой рубиновый код:
categories_controller.rb
def create
@category = Category.new(category_params)
respond_to do |format|
if @category.save
ActionCable.server.broadcast 'categories', { category: @category }
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
Мой кабель.имл
development:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
У вас есть идея, чтобы помочь мне?
Хорошего дня.