[英]is it possible to call a rails helper method from within javascript?
我的application.html.erb [查看文件]中有一個javascript代碼塊,它從外部api接收事件。 我要做的就是在javascript代碼塊中收到事件時,我需要在[application_helper.rb]中執行一個輔助方法。
我在這里瀏覽了大多數te堆棧,但是找不到任何相關答案。
javascript代碼塊如下所示:
var pusher = new Pusher('b00c9657a0a896f3ec26');
var channel = pusher.subscribe('NewOrders');
channel.bind('NewOrderArrived', function(data) {
[ I NEED TO CALL THE RAILS HELPER METHOD HERE ]
console.log("got u")
});
不直接,不。 正如Dave Newton在評論中所說,Ruby在服務器上執行,而JS在客戶端上執行。 您想要做的是這樣的:
var pusher = new Pusher('b00c9657a0a896f3ec26');
var channel = pusher.subscribe('NewOrders');
channel.bind('NewOrderArrived', function(data) {
$.get('<%= url_for :some_controller_method_that_calls_helper %>', function(response){
// the response variable will contain the output of the controller method
});
console.log("got u")
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.