LoginSignup
27
23

More than 5 years have passed since last update.

Railsでajax:successのイベントがとれなかった

Last updated at Posted at 2013-09-10

rails 3.2.13
jquery-rails 3.0.4

Rails 3系で簡単に非同期ボタン実装しようとするとこんな感じに出来て便利。

= link_to 'hogehoge', hogehoge_path, remote: true, id: 'hogehoge_link'

完了時に何か処理をしたくてイベントをとりたいとき

ajax:successというイベントが使える。

詳しくは、Github rails/jquery-ujs Custom events fired during "data-remote" を参照のこと

$ ->
  $('#hogehoge_link').on('ajax:success', (event, data, status)->
    $(@).text('done!!')
  )

便利便利。

ところが、ある日動かなくなって

ajax:completeはとれるので、statusみたら、"parseerror"吐いてた。

Railsのアクションで

class HogeController < ApplicationController
  def hogehoge
     # do hogehoge
    render nothing: true, status: 200
  end
end

みたいなことしてた。
nothing: trueだとなんも出ないんで、"parseerror"になって、HTTP statusが200じゃなくてもajax:successがfireされないっぽい。ハマった。

def hogehoge
  render json: @hoge, status: 200
end

とかにすれば動いたね。なかなか気づくのに苦労したのでメモしておく。

27
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
23