###前提・実現したいこと
Action Cableを使ってmessageとroomを紐付けたいのですが、どうすればいいですか?ログによると、RoomChannelのsubscribedメソッドのparamsで値が渡せてなさそうです。どうすればいいですか?
参考にしたページは以下になります。
https://qiita.com/kohei1228/items/7aed5aad9c63e834c0e1
Room_idを受け取るからうまくいかなくなりました。
###発生している問題・エラーメッセージ
Room Load (1.1ms) SELECT `rooms`.* FROM `rooms` WHERE `rooms`.`id` = NULL LIMIT 1 Could not execute command from ({"command"=>"message", "identifier"=>"{\"channel\":\"RoomChannel\"}", "data"=>"{\"message\":\"canada\",\"action\":\"speak\"}"}) [ActiveRecord::RecordNotFound - Couldn't find Room with 'id'=]: /Users/taichi1009/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.1.4/lib/active_record/core.rb:189:in `find' | /Users/taichi1009/Desktop/setuplove/app/channels/room_channel.rb:16:in `speak' | /Users/taichi1009/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actioncable-5.1.4/lib/action_cable/channel/base.rb:262:in `public_send' | /Users/taichi1009/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actioncable-5.1.4/lib/action_cable/channel/base.rb:262:in `dispatch_action' | /Users/taichi1009/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actioncable-5.1.4/lib/action_cable/channel/base.rb:166:in `block in perform_action' Processing by RoomsController#show as HTML Parameters: {"id"=>"1"}
###該当のソースコード
class RoomChannel < ApplicationCable::Channel def subscribed # stream_from "room_channel" binding.pry stream_from "room_channel_#{params['room_id']}" end def unsubscribed # Any cleanup needed when channel is unsubscribed end def speak(data) # ここでRoomとMessageが紐付けられる。 # Message.create! content: data['message'], user_id: current_user.id, room_id: 1 binding.pry Message.create(content: data['message'], user_id: current_user.id, room_id: Room.find(params['id'])) # Message.create(content: data['message'], sent_user: current_user.id, room: Room.find(params['room_id'])) # ActionCable.server.broadcast 'room_channel', message: data['message'] end end
###試したこと
binding.pryで止めたのですが、以下の値しか取得できませんでした。
paramsにroom_idが入っていません。
[1] pry(#<RoomChannel>)> params
=> {"channel"=>"RoomChannel"}
###補足情報(言語/FW/ツール等のバージョンなど)
rails5.1.4
Redis 3.0.6
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。