首页 > 解决方案 > 刺激反射错误调用动作“change->stimulus-reflex#__perform”

问题描述

我在heroku上使用rails 6,当我执行反射动作时,控制台记录:

当我选择一个单选按钮时,我尝试创建一个 Mifal 我该怎么办?

Error invoking action "change->stimulus-reflex#__perform"

"The ActionCable connection is not open! `this.isActionCableConnectionOpen()` must return true before calling `this.stimulate()`"

编码:

more_info.html.erb:

 <div class='preps'>
    <%= f.input :heads_prep,  label_html: {class:"label"}, item_wrapper_class: "radio-label", input_html: {data: {reflex:"change->preps#toggle_prep", "prepValue": 'heads', activity: @activity.id}},  collection: Activity.heb_preps.map{ |k, v| [v, k] }, as: :radio_buttons %>
    <%= f.input :guides_prep, label_html: {class:"label"}, item_wrapper_class: "radio-label", input_html: {data: {reflex:"change->preps#toggle_prep", "prepValue": 'guides', activity: @activity.id}}, collection: Activity.heb_preps.map{ |k, v| [v, k] }, as: :radio_buttons %>
  </div>

preps_reflex.rb:

# frozen_string_literal: true

class PrepsReflex < ApplicationReflex
  delegate :current_user, to: :connection

  def toggle_prep
    activity = Activity.find(element.dataset.activity)
    prep = element.dataset.prepvalue
    if element.value == 'yes'
      puts prep
      activity.update("#{prep}_prep": :yes)
      unless activity.heads_mifal.present?
        activity.mifals.create(name: "#{Mifal.heb_mifal_type(prep)} #{activity.name}", mifal_type: prep)
      end
    else
      activity.update("#{prep}_prep": :no)
      activity.mifals.find_by(mifal_type: prep).destroy
    end
    # morph "#preps", "Your muscles... they are so tight."

  end
end

我使用设计:connection.rb:

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    protected

    def find_verified_user
      if (current_user = env["warden"].user)
        current_user
      else
        reject_unauthorized_connection
      end
    end

  end
end

标签: ruby-on-railsherokustimulusjsstimulus-reflex

解决方案


推荐阅读