How can I redirect to a path from a n_channel.rb?
I can't use redirect_to
or link_to
.
What is the best way?
How can I redirect to a path from a n_channel.rb?
I can't use redirect_to
or link_to
.
What is the best way?
Your channel serves as a connection, so you actually do that from some other part of your application.
Assuming you've connected everything properly, you can do the following.
In the aforementioned other part of your application, when the time is ripe for redirect:
def async_redirect(path)
NChannel.broadcast_to(
user, # or however you identify your subscriber
head: 302, # redirection code, just to make it clear what you're doing
path: path # you'll need to use url_helpers, so include them in your file
)
end
And then on the front
App.cable.subscriptions.create("NChannel", {
connected: () => {},
received: (data) => {
if (data.head == 302 && data.path) {
window.location.pathname = data.path; # Voila
}
}
});
Redirections are not supposed to be done in any model files(n_channel.rb seems model file).
The ideal way is to return your value from model to controller. Then depending on your business needs have redirections from controller.