Jquery web推送serviceworker未定义的方法错误?

Jquery web推送serviceworker未定义的方法错误?,jquery,ruby-on-rails,ruby,web-push,Jquery,Ruby On Rails,Ruby,Web Push,每次我尝试启动我的服务器时,我都会遇到此错误 undefined method `post' for main:Object (NoMethodError) 应用程序.rb post "/push" do Webpush.payload_send( endpoint: "https://fcm.googleapis.com/gcm/send/eah7hak....", message: "A message", p256dh: "BO/aG9nYXN

每次我尝试启动我的服务器时,我都会遇到此错误

undefined method `post' for main:Object (NoMethodError)
应用程序.rb

post "/push" do
    Webpush.payload_send(
      endpoint: "https://fcm.googleapis.com/gcm/send/eah7hak....",
      message: "A message",
      p256dh: "BO/aG9nYXNkZmFkc2ZmZHNmYWRzZmFl...",
      auth: "aW1hcmthcmFpa3V6ZQ==",
      vapid: {
        subject: "mailto:sender@example.com",
        public_key: ENV['VAPID_PUBLIC_KEY'],
        private_key: ENV['VAPID_PRIVATE_KEY']
      }
    )
end
我把代码块放错地方了吗?我假设
app.rb
application.rb

截图来自git教程:

我正在尝试用乏味的gems和webpush实现浏览器通知

application.js

// Register the serviceWorker script at /serviceworker.js from our server if supported
if (navigator.serviceWorker) {
  navigator.serviceWorker.register('/serviceworker.js')
  .then(function(reg) {
    console.log('Service worker change, registered the service worker');
  });
}
// Otherwise, no push notifications :(
else {
  console.error('Service worker is not supported in this browser');
}

// When serviceWorker is supported, installed, and activated,
// subscribe the pushManager property with the vapidPublicKey
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
  serviceWorkerRegistration.pushManager
  .subscribe({
    userVisibleOnly: true,
    applicationServerKey: window.vapidPublicKey
  });
});

$('.webpush-button').on('click', (e) => {
  navigator.serviceWorker.ready
  .then((serviceWorkerRegistration) => {
    serviceWorkerRegistration.pushManager.getSubscription()
    .then((subscription) => {
      $.post('/push', {
        subscription: subscription.toJSON(),
        message: 'You clicked a button!'
      });
    });
  });
});

// Let's check if the browser supports notifications
if (!("Notification" in window)) {
  console.error("This browser does not support desktop notification");
}

// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
  console.log("Permission to receive notifications has been granted");
}

// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
  Notification.requestPermission(function (permission) {
  // If the user accepts, let's create a notification
    if (permission === "granted") {
      console.log("Permission to receive notifications has been granted");
    }
  });
}

将其添加到控制器中的简单示例

首先,使用操作的名称创建控制器(我将使用
push
,以遵循您的示例):

这将创建一个测试控制器并更新您的路由:

# app/controllers/test_controller.rb

class TestController < ApplicationController
  def push
  end
end

# config/routes.rb

get 'test/push'

这样,您就有了一个POST-yoursite/test/push端点(就像
app.rb
的例子一样)。

是的,您把代码放错地方了,
app.rb
不适用于rails应用程序。尝试在rails控制器中使用该代码。这完全取决于rails设计,如果您想成为根页面,请创建根路由,否则任何路由都可以。方法名(aka action)也是如此。是的,您没有向您的操作发送任何参数,因此
params
hash是空的,因此,当尝试获取
params[:subscription][:endpoint]
时,您会得到一个
NoMethodError
,因为
params[:subscription]
nil
nil
不响应
[]
方法(即
[:端点]
)。使用
form\u作为
链接,而不是使用
form\u作为
链接,或者使用带有所需参数的简单html表单。我尝试了
form\u作为
链接,但仍然出现错误。这些值应该来自gems,而不是我,所以我对如何继续有点困惑。我在这里问了一个新问题:。。。如果您有时间继续对话:)再次感谢您的帮助!
# app/controllers/test_controller.rb

class TestController < ApplicationController
  def push
  end
end

# config/routes.rb

get 'test/push'
class TestController < ApplicationController
  def push
    Webpush.payload_send(
      message: params[:message],
      endpoint: params[:subscription][:endpoint],
      p256dh: params[:subscription][:keys][:p256dh],
      auth: params[:subscription][:keys][:auth],
      vapid: {
        subject: "mailto:sender@example.com",
        public_key: ENV['VAPID_PUBLIC_KEY'],
        private_key: ENV['VAPID_PRIVATE_KEY']
      }
    )
  end
end
post 'test/push'