在我的控制器操作中,我初始化一個數組的會話並插入值。這些值是通過ajax從客戶端獲得的,因此在將這些值插入到數組中時不會刷新頁面。但令人驚訝的是,每次它都會初始化新會話而不是插入到相同的已定義會話中。這裏是我的代碼值不保存在會話數組中
控制器
def receive_tags
parser = Yajl::Parser.new
@hash = parser.parse(request.body.read)
log=Logger.new(STDOUT)
log.error(@hash)
session[:tags]||=[]
session[:tags] << @hash["tag"]
unless session[:tags].empty?
log.error(session[:tags]) #this keeps printing the current value i was expecting it to print a list of values including the previous
end
render :nothing=>true
end
阿賈克斯
var myobj={tag:"mytag"};
$.ajax({
url: 'ips/receive_tags',
type: 'post',
contentType: 'application/json; charset=UTF-8',
accept: 'application/json',
dataType: 'json',
data:JSON.stringify(myobj),
success: function(res) {
if (res.ImportResponse !== void 0) {
console.log('Success: ' + res);
} else if (res.Fault !== void 0) {
console.log('Fault: ' + res);
}
},
error: function() {
console.error('error!!!!');
}
});
請縮進代碼。沒有人希望讀取不帶有縮進的代碼。 – meagar