railsでjQueryのliveメソッドからajax使うとエラーが出る
dotインストールを使ってrailsの勉強をしていたらハマったので記録
http://dotinstall.com/lessons/basic_rails/6535
railsには最初からjqueryが入ってるのでajaxが簡単に使えるんですねやったー
と意気揚々とソースコード丸写ししてたらchromeさんがエラーをはいてた
Uncaught TypeError: Object [object Object] has no method 'live' posts:46 (anonymous function) posts:46 fire jquery.js:3075 self.fireWith jquery.js:3187 jQuery.extend.ready jquery.js:434 completed jquery.js:105
解決
該当箇所はここでした。jQueryのliveメソッドを使っているのがいけないらしい
$('a[data-method="delete"]').live('ajax:success', function(e, data, status,xhr){ $('#post_'+data.post.id).fadeOut("slow"); });
->
$('a[data-method="delete"]').on('ajax:success', function(e, data, status,xhr){ $('#post_'+data.post.id).fadeOut("slow"); });
結論
jQuery 1.9以降のバージョンではliveというメソッドがなくなっているので使ってはいけないとの事
railsはjquery-railsというものでjQueryのバージョンを指定しているらしい。
railsのバージョンによってはデフォルトで1.9より後になるので、注意が必要。