在我的application_controller中。在javascript條件下使用rails實例變量
def ensure_service_agreement
if current_user.agrees_to_service == true
@tos = true
else
@tos = false
end
end
我在我的application.js文件中有以下代碼。
var tos = "<%= @tos %>";
if(tos == false){
$(".new-session-home").on('click', function(){
$('.booking.modal')
.modal('show');
})
}else{
$(".new-session-home").on('click', function(){
$('.tos.modal')
.modal('show');
})
}
JavaScript似乎沒有註冊var tos。當我將第一個if語句設置爲true或false時,我得到第二個模態(tos模式)
如果我設置tos = 4然後檢查那個數字,它就像它應該那樣工作,所以我知道這個問題是與rails實例變量。
如何獲得JS來理解我的實例變量?
它是js文件,它無法讀取erb語法.. – Nithin 2014-12-08 06:46:13
這是我的問題,我需要coffeescript呢? – Peege151 2014-12-08 06:49:26
noeps,你需要controller_action_name.js。erb – argentum47 2014-12-08 06:59:57