2012-03-13 12 views
3

my Rails appに2か所がajaxコールを持っています。 (1つはjQueryUIドラッグアンドドロップソート、もう1つはコメント投稿の更新)。Ajax PUTリクエストは、Railsアプリケーションでログアウトします。どうして?

これらの呼び出しが発生するたびに、ユーザーはログアウトします。明白な理由なしに。 私はomniauth-facebookとomniauth-google-oauth2を使って認証しています。

これはどのように修正できますか?ここで

は、AJAX呼び出しが(CoffeeScriptの)次のようになります。

$.ajax({ 
    type: 'put', 
    data: {post_id: post.attr("id")}, 
    dataType: 'json', 
    complete: -> post.children('.headpost').children('.buttons').removeClass('new_reply'), 
    url: '/posts/update/'}) 

ありがとう!

+0

のhttp:/ /weblog.rubyonrails.org/2011/2/8/csrf-protection-bypass-in-ruby-on-rails/ 私のajaxリクエストに信頼性トークンがないので、セッションがリセットされます。さて、アセットのcoffeescriptファイルでajaxリクエストでそれを行う方法... – Arcolye

答えて

3

私は何をやってしまった:

をapplication.html.erbレイアウトヘッドでは、<%= csrf_meta_tags %>下:

<%= javascript_tag "var AUTH_TOKEN = '#{form_authenticity_token}';" if protect_against_forgery?%> 
資産/ whatever.js.coffeeで

$.ajax ({ 
      type: 'put', 
      data: {authenticity_token: AUTH_TOKEN}, 
      dataType: 'json', 
      complete: -> post.children('.headpost').children('.buttons').removeClass('new_reply'), 
      url: '/posts/'+post.attr("id").slice(5) }); 
+0

ありがとうhttp://stackoverflow.com/questions/7560837/proper-way-to-send-an-authenticity-token-with-ajax – Arcolye

関連する問題