问题描述
我有一个博客并使用stimulus_reflex 来批准/不批准对帖子的评论。我也用它来发布/取消发布帖子。它在本地主机上工作正常,并且在生产服务器上工作,但它不再工作。我所做的唯一一件事就是使用 certbot 安装 ssl 证书。它停止工作,所以我完全从服务器中删除了 certbot,但它仍然没有工作。我已经重新安装了 ssl 证书。
<% 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 %>
这是publish_reflex.rb 字段:
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
在我在这里找到的帖子中,我尝试创建一个 initializers/warder_cookies.rb 文件:
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,opts|
scope = opts[:scope]
auth.cookies.signed["#{scope}.id"] = nil
auth.cookies.signed["#{scope}.expires_at"] = nil
end
并且帖子说要添加以下代码但没有给出文件所以我搜索并将其添加到channels/connection.rb文件中:
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
当我查看 Nginx 服务器日志时,我得到了这个:
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,host: "allaboutjudo.com"
在本地主机上,当我打开控制台时,我看到:
ActionCable is connected
Error invoking action "click->stimulus-reflex#__perform"
The ActionCable connection is not open! `this.isActionCableConnectionopen()` must return true before calling `this.stimulate()`
解决方法
在我了解了stimulus_reflex 的教程中,它从未提到必须运行redis 服务器。我在刺激反射的快速入门指南中看到它必须安装并运行,所以我登录到服务器并运行命令
$ redis-server
现在反射起作用了!