I have searched the web far and wide (including reading many code examples for ActionCable
) for what seems to be an answer to a very basic question. Alas, I have not solved my problem.
Suppose, I have a model Search
and I have navigated to the URL /searches/1
.
I would also have the search.coffee
file under javascripts/channels/
which starts with:
App.instance = App.cable.subscriptions.create 'SearchChannel'
and a SearchChannel
class that looks like this:
class SearchChannel < ApplicationCable::Channel
def subscribed
search = Search.find(params[:id])
stream_for search
end
def unsubscribed
end
end
Naturally, the code above produces an error because params[id]
is nil
.
Here are my questions:
- How do I subscribe to the correct
Search
instance based on the URL? - How do I avoid trying to subscribe to
SearchChannel
if I am on another URL that doesn't require a subscription, e.g./searches/1/results
?
Thank you for help!