I have a blog and use stimulus_reflex to approve/unapprove comments on posts. I also use it to publish/unpublish posts. It works fine on the localhost and was working on the production server but it is no longer working. The only thing I did was install an ssl certificate with certbot. It stopped working so I remove certbot from the server completely but it still did not work. I have reinstalled the ssl certificate.
Here is the code for the post edit page for the publish and unpublish buttons:
<% if @post.published? %>
<a href="#"
class='btn btn-secondary btn-block'
data-reflex='click->PublisherReflex#unpublish'
data-post-id='<%= @post.id %>'>
Unpublish
</a>
<% else %>
<a href="#"
class='btn btn-dark btn-block'
data-reflex='click->PublisherReflex#publish'
data-post-id='<%= @post.id %>'>
Publish
</a>
<% end %>
Here is the publish_reflex.rb fild:
class PublisherReflex < ApplicationReflex
def publish
post = Post.find(element.dataset[:post_id])
post.update(published: true, published_at: Time.now)
end
def unpublish
post = Post.find(element.dataset[:post_id])
post.update(published: false, published_at: nil)
end
end
On post I found on here I tried creating a initializers/warder_cookies.rb file:
Warden::Manager.after_set_user do |user, auth, opts|
scope = opts[:scope]
auth.cookies.signed["#{scope}.id"] = user.id
auth.cookies.signed["#{scope}.expires_at"] = 30.minutes.from_now
end
Warden::Manager.before_logout do |_user, auth, opts|
scope = opts[:scope]
auth.cookies.signed["#{scope}.id"] = nil
auth.cookies.signed["#{scope}.expires_at"] = nil
end
And the post said to add the following code but didn't give a file so I searched and added it to the channels/connection.rb file:
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
# rubocop:disable Lint/AssignmentInCondition
if verified_user = User.find_by(id: cookies.signed['user.id'])
verified_user
else
reject_unauthorized_connection
end
end
end
end
When I view the nginx server log I get this:
2021/03/28 16:11:34 [error] 1365851#1365851: *131507 upstream prematurely closed connection while reading response header from upstream, client: 2601:282:1880:3a10:e5b0:e2df:dbe6:9c58, server: allaboutjudo.com, request: "GET /cable HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.D7yaVlf/agents.s/core:", host: "allaboutjudo.com"
2021/03/28 16:11:40 [error] 1365851#1365851: *131515 upstream prematurely closed connection while reading response header from upstream, client: 98.38.139.55, server: allaboutjudo.com, request: "GET /cable HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.D7yaVlf/agents.s/core:", host: "allaboutjudo.com"
On the localhost when I open the console I see:
ActionCable is connected
But on the production site if I click the unpublish button I get this error:
Error invoking action "click->stimulus-reflex#__perform"
The ActionCable connection is not open! `this.isActionCableConnectionOpen()` must return true before calling `this.stimulate()`
In the tutorial I learned about stimulus_reflex it never mentioned that the redis server had to be running. I saw in a quickstart guide to stimulus reflex that it must be installed and running so I logged into the server and ran the command
and now the the reflexes work!