2017-04-23 2 views
-2

私はこのエラーを取得する私のサーバーを起動しようとするたびに...ウェブプッシュserviceworker未定義メソッドエラー?

undefined method `post' for main:Object (NoMethodError) 

application.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:[email protected]", 
     public_key: ENV['VAPID_PUBLIC_KEY'], 
     private_key: ENV['VAPID_PRIVATE_KEY'] 
     } 
    ) 
end 

私は間違った場所にコードの塊を入れているだろうか? https://github.com/zaru/webpush:私はapp.rb

enter image description here

スクリーンショットは、Gitのチュートリアルからであるapplication.rb

と同じものであると仮定します。

私はVAPIDと宝石でブラウザ通知を実装しようとしていますwebpush & serviceworker-railsコントローラにそれを追加する

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"); 
    } 
    }); 
} 
+2

はい、あなたは間違った場所にコードを置いています、 'app.rb'はレールアプリ用ではありません。そのコードをレールコントローラで使用してみてください。 – Gerry

+1

あなたのルートページになりたいならば、それはあなたのレールデザインに依存し、ルートルートを作成してください。同じことがメソッド名(別名アクション)と同じです。 – Gerry

答えて

1

簡単な例。

まず、アクションの名前(私はあなたの例に従うことを、pushを使用します)とコントローラを作成:テストコントローラを作成し、あなたのルートを更新します

$ rails g controller Test push 

を:

# 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:[email protected]", 
     public_key: ENV['VAPID_PUBLIC_KEY'], 
     private_key: ENV['VAPID_PRIVATE_KEY'] 
     } 
    ) 
    end 
end 

そしてアップ:あなたが追加したいコードを

次の更新コントローラ日付あなたのルートがpost代わりのgetを使用する:

それと
post 'test/push' 

あなたが(ちょうどapp.rbとの一例として)利用可能エンドポイントをプッシュ/ POST yoursite /テストを持っています。

+0

はい、アクションにパラメータを送信していないので、 'params'ハッシュは空です。' params [:subscription] [:endpoint] 'を取得しようとすると、' params [サブスクリプション]は 'nil'で、' nil'は '[]'メソッド(つまり '[:endpoint]')に応答しません。 'link_to'の代わりに' form_for'を使うか、必要なパラメータを持つ単純なhtmlフォームを使います。 – Gerry

+0

私は 'form_for'と' <%= link_to 'send'、push_path、{message: 'YEAAA!'、エンドポイント: 'value2'、p256dh: 'value1'、認証: 'value2'、サブスクリプション: 'value1'キー: 'value2'、メソッド::投稿}%> 'しかし、私はまだエラーが発生します。値は、私とは違って、宝石から来ているはずなので、どうやって進めるのかちょっと混乱しています。私はここで新しい質問をしました:http://stackoverflow.com/questions/43572177/subscription-params-nil-for-web-push-serviceworker ...あなたは会話を続ける時間があれば:)あなたのためにもう一度ありがとう助けて! –

関連する問題