2014-11-24 22 views
17

我試圖給我的網站上的用戶發送關於品牌名稱的「點數」或「點數」。Rails中的422(不可處理的實體)?由於路線或控制器?

我有合適的視圖看中Twitter的部件...

<p><a href="https://twitter.com/share" class="twitter-share-button" data-text="Check Out This Awesome Website Yay" data-via="BrandName" data-hashtags="ProductName">Tweet</a> 
<div id="credited"></div> 
<script>window.twttr = (function (d, s, id) { 
    var t, js, fjs = d.getElementsByTagName(s)[0]; 
    if (d.getElementById(id)) return; 
    js = d.createElement(s); js.id = id; 
    js.src= "https://platform.twitter.com/widgets.js"; 
    fjs.parentNode.insertBefore(js, fjs); 
    return window.twttr || (t = { _e: [], ready: function (f) { t._e.push(f) } }); 
}(document, "script", "twitter-wjs")); 
</script>  

我對JS都寫了漂亮....

function creditTweet() { 
    $.post(
    "/credit_tweet", 
    {}, 
    function(result) { 
     var text; 
     if (result.status === "noop") { 
     text = "Thanks for sharing already!"; 
     } else if (result.status === "ok") { 
     text = "5 Kredit Added"; 
     } 
     $("#credited").html(text); 
    } 
); 
} 

$(function() { 
    twttr.ready(function (twttr) { 
    window.twttr.events.bind('tweet', creditTweet); 
    }); 
}); 

現在的問題是無論是在控制器OR中的路線(我發佈的地方)。我認爲路由很好,因爲POST幾乎可以工作,因爲這是對維基百科上的錯誤的描述 - 「422 Unprocessable Entity(WebDAV; RFC 4918) 請求格式正確但由於語義而無法跟蹤錯誤「。

那麼,你們看到我的控制器中的ruby代碼有什麼問題嗎?

class SocialKreditController < ApplicationController 
    TWEET_CREDIT_AMOUNT = 5 

    def credit_tweet 
    if !signed_in? 
     render json: { status: :error } 
    elsif current_user.tweet_credited 
     Rails.logger.info "Not crediting #{ current_user.id }" 
     render json: { status: :noop } 
     else 
     Rails.logger.info "Crediting #{ current_user.id }" 
     current_user.update_attributes tweet_credited: true 
     current_user.add_points TWEET_CREDIT_AMOUNT 
     render json: { status: :ok } 
     end 
    end 
end 

而且在我的routes.rb,這是很簡單的,所以我懷疑有什麼錯在這裏...

get 'social_kredit/credit_tweet' 
    post '/credit_tweet' => 'social_kredit#credit_tweet' 

在哪裏哦,這是哪裏的錯誤?我顯然不知道HTTP請求。

+0

當ActiveRecord對象無法更新/保存時,IME欄通常返回422。你的日誌說什麼? – ihaztehcodez 2014-11-24 05:30:35

+0

看起來像.... 開始POST「/ credit_tweet」for 127.0.0.1於2014-11-24 00:34:53 -0500 正在處理SocialKreditController#credit_tweet as */* 無法驗證CSRF令牌的真實性 已完成422 1ms內無法處理的實體 ActionController :: InvalidAuthenticityToken - ActionController :: InvalidAuthenticityToken: – piratetone 2014-11-24 05:35:27

+0

您可以使用顯示此錯誤的行從rails服務器發佈日誌嗎?另外,爲什麼你有兩個路線'get'和'post'指向相同的方法? – Surya 2014-11-24 05:45:12

回答

38

我明白了!

我加了一個...

skip_before_action :verify_authenticity_token 

到控制器。

檢出日誌並發現無法驗證CSRF令牌時發現該問題。

+9

您可能還想查看[此問題](http://stackoverflow.com/questions/7203304/warning-cant-verify-csrf-token-authenticity-rails)解決方案,不需要禁用csrf令牌檢查。 – ihaztehcodez 2014-11-24 05:52:11

+0

謝謝@ihaztehcodez - 此skip_before_action:verify_authenticity_token僅用於登錄後用戶所必需的控制器。是否有任何主要原因可能導致我想使用此鏈接中提供的AJAX解決方案通過此小代碼段? – piratetone 2014-11-24 06:11:41

+8

@piratetone:是的,這就是安全。 – Surya 2014-11-24 06:15:05

1

我面對同樣的問題。 它添加

skip_before_action後挑選出:verify_authenticity_token

在你的控制器在您的JS呼籲或發送數據的頂部。

class UserController < ApplicationController 
    skip_before_action :verify_authenticity_token 
    def create 
    end 
end 

如代碼段所示。

相關問題