0

我有一个博客并使用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, auth, 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, server: allaboutjudo.com, request: "GET /cable HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.D7yaVlf/agents.s/core:", 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()` 
4

1 回答 1

0

在教程中我了解了stimulus_reflex,它从未提到redis 服务器必须运行。我在刺激反射的快速入门指南中看到它必须安装并运行,所以我登录到服务器并运行命令

$ redis-server 

现在反射起作用了!

于 2021-04-11T01:11:12.773 回答